Node.js Axios 使用 cookie 向 API 发送请求

羊奶

我想在 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 中的拼写错误,尽管接受的答案是最终解决方案。

冰山

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

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

axios.post() 签名

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

axios post 方法

顺便说一句,Cookie无论您使用本机 fetch 还是浏览器端 axios,在浏览器端覆盖标头都不会按设计工作,请参阅Forbidden header name

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

(仅限Safari)Axios请求不会将cookie发送到Node.js / Express REST API

使用AXIOS(Node.js)在请求之间保留cookie

Chrome 扩展:向 node.js 服务器发出 http 请求,发送 cookie 凭据(CORS)

在Express.js上使用Axios向Spotify API发出POST请求时出现错误400

读取超时。向node.js API发送POST请求时出错

使用 localhost 向 API 发送 POST 请求

使用 Alamofire 向 API 发送 POST 请求

如何使用 axios 向 jwt-auth 发送 GET 请求?

尝试使用 Axios 向 API 发送 POST 请求时,响应中的“Access-Control-Allow-Credentials”标头为“ ”

在Vue.js中使用axios将get请求发送到Laravel API

Angular向Node.js的发布请求未发送

Node.js请求模块....在主体中发送JSON以使用api请求进行API请求

向第三方API Node.js发出多个请求后如何发送响应

从node.js向WMATA API发出获取请求

向Node.js中的JSON API发出GET请求?

使用Axios使用node.js进行Face API

Axios.Post 不会向在 node.js 上工作的 mongodb 发送数据

如何使用邮递员向Timekit API发送请求

使用 Python 向 mailchimp API 发送 POST 请求

如何使用 angular 向 REST api 发送删除请求

在React.js中使用axios发送POST请求的问题

从URL提取令牌并使用Axios发送发布请求-Vue js

Selenium JS向请求添加cookie

使用 vanilla JS 向 Django 视图发送 AJAX 请求

使用香草JS XMLhttprequest对象向Rails发送“发布”请求

使Axios自动在其请求中发送cookie

使用node.js发送请求百度地图API得到错误的响应?

如何使用 node js 向 YouTube 发送直播

使用Prometheus监视Node.js axios请求