【问题标题】:Node.js Axios post request to API with cookieNode.js Axios 使用 cookie 向 API 发送请求
【发布时间】:2021-10-30 18:22:21
【问题描述】:

我想在 Axios 的 POST 请求中提供 cookie。我有一个与 CURL 一起使用的 zeppelin API:

curl -i --data 'userName=admin&password=admin' -X POST http://zeppelin.XXXXX.net/api/login

和回应

HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: authorization,Content-Type
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, HEAD, DELETE
Content-Length: 127
Content-Type: application/json
Date: Wednesday, September 1, 2021 8:20:55 AM UTC
Server: Jetty(9.4.14.v20181114)
Set-Cookie: rememberMe=deleteMe; Path=/; Max-Age=0; Expires=Tue, 31-Aug-2021 08:20:55 GMT
Set-Cookie: JSESSIONID=a3a4392d-1347-47b6-b85b-61230e16802b; Path=/; HttpOnly
Set-Cookie: JSESSIONID=deleteMe; Path=/; Max-Age=0; Expires=Tue, 31-Aug-2021 08:20:55 GMT
Set-Cookie: rememberMe=deleteMe; Path=/; Max-Age=0; Expires=Tue, 31-Aug-2021 08:20:55 GMT
Set-Cookie: JSESSIONID=14acf05d-3cb3-4548-b8e3-6066caf90000; Path=/; HttpOnly
Set-Cookie: rememberMe=deleteMe; Path=/; Max-Age=0; Expires=Tue, 31-Aug-2021 08:20:55 GMT
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 1

在 zeppelin 中使用音符

curl -i -b 'JSESSIONID=14acf05d-3cb3-4548-b8e3-6066caf90000; Path=/; HttpOnly' -X POST http://zeppelin.XXXXXX.net/api/notebook/job/2G9P1992Z/20210615-091750_1582099873

成功

HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: authorization,Content-Type
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, HEAD, DELETE
Content-Length: 15
Content-Type: application/json
Date: Wednesday, September 1, 2021 8:28:07 AM UTC
Server: Jetty(9.4.14.v20181114)
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 1

我有一个登录和获取cookie的功能

const axios = require('axios')
const setCookie = require('set-cookie-parser');

const zeppelinLoginCoockie = async function(){
    return new Promise((resolve, reject) => {
        axios
        .post(process.env.ZEPPELIN_URL + 'login', 'userName=admin&password=admin')
        .then(res => {
            console.log(`statusCode: ${res.status}`);
            //console.log(res);
            let cookies = setCookie.parse(res, {
                decodeValues: true  // default: true
              });
            let lastSessionId = 'null';
            cookies.forEach(element => {
                if ( element.name === 'JSESSIONID' ) {
                    lastSessionId = element.value;
                };
            });
            //console.log(cookies);
            return resolve(lastSessionId);
        })
        .catch(error => {
            console.error(error);
            return reject(error);
        });
    });
}

这可行,但现在我不知道如何执行第二个命令来启动便笺。

我尝试提供 cookie,如下所示:

const zeppelinNoteRun = async function(loginCookie){
    return new Promise((resolve, reject) => {
        const cookieHeader = `JSESSIONID=${loginCookie}; Path=/; HttpOnly`
        axios
        .post(process.env.ZEPPELIN_URL + 'notebook/job/2G9P1992Z/20210615-091750_1582099873', {
            headers:{
                cookie: cookieHeader
            } 
        })
        .then(res => {
            console.log(`statusCode: ${res.status}`);
            return resolve(res);
        })
        .catch(error => {
            console.error(error);
            return reject(error);
        });
    });
}

我不精通 Axios 或 cookie,所以我很想知道如何执行这样的任务?

编辑:节点中 url 的错字,尽管接受的答案是最终解决方案。

【问题讨论】:

  • 我读过它只适用于浏览器。但是现在尝试了这个,我得到了Error: Request failed with status code 405

标签: javascript node.js axios


【解决方案1】:

您已将包含 cookie 的对象作为 POST 数据传递。 尝试在 axios.post 的第一个参数后添加一个 null

axios.post(url, null, {
  headers:{
    cookie: cookieHeader
  } 
})

axios.post() 签名

axios#post(url[, data[, config]])

axios post method

顺便说一句,在浏览器端覆盖 Cookie 标头将无法正常工作,无论您使用本机 fetch 还是浏览器端 axios,请参阅 Forbidden header name

【讨论】:

  • 试过了,现在 cookie 是正确的,但我得到 404 和 Cookie: 'JSESSIONID=c0017ad6-d120-46bc-8fd6-a54d0f0275c7; Path=/; HttpOnly', 'User-Agent': 'axios/0.21.1' 使用 curl 我得到 Cookie: JSESSIONID=14acf05d-3cb3-4548-b8e3-6066caf90000; Path=/; HttpOnly 和 --verbose(不同的实例,所以 diff cookie)
  • @eemilk 发出请求时不要在cookie中添加路径信息,请使用cookie: 'a=1; b=2' plz。
  • 还是 404。我已经厌倦了 JSESSIONID=${loginCookie}loginCookieJSESSIONID=${loginCookie}; HttpOnly
  • @eemilk 完整日志??
猜你喜欢
  • 2020-07-07
  • 2023-03-31
  • 2019-11-06
  • 2014-04-16
  • 1970-01-01
  • 2018-07-11
  • 2019-09-27
  • 1970-01-01
  • 2021-11-12
相关资源
最近更新 更多