Reputation: 1628
I want to provide cookie in POST request with Axios. I have a zeppelin API which works with CURL as:
curl -i --data 'userName=admin&password=admin' -X POST http://zeppelin.XXXXX.net/api/login
and response
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
To use a note in 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
succeeds with
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
I have a function to login and get the 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);
});
});
}
which works, but now I don't know how to do the second command to launch the note.
I tried to provide cookie as in:
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);
});
});
}
I am not proficient with Axios or cookies, so I'm interested to know how to perform such a task?
EDIT: a typo in url in node, though the accepted answer was the final solution.
Upvotes: 0
Views: 849
Reputation: 3332
You've passed the object containing cookie as the POST data. Try to add a null after the 1st argument of axios.post
axios.post(url, null, {
headers:{
cookie: cookieHeader
}
})
axios#post(url[, data[, config]])
BTW, overwriting the Cookie
header in browser side won't work by design, no matter you use native fetch or browser side axios, see Forbidden header name.
Upvotes: 1