// Require the request
const request = require('request');
let api_url = 'https://some-api-that-requires-cookies.com'
// Setup the cookie jar to carry cookies
const cookieJar = request.jar();
let cookie = request.cookie('data=3');
cookieJar.setCookie(cookie);
// Add your cookie to the jar (URL is parsed into cookie parts)
// Send your request and include the cookie jar
request({url: api_url, jar: cookieJar},
(error, response, body)=>{
// do things
console.log(response.headers['set-cookie'])
}
);
Output:
[
'sess_id=D8w23-9%!3832ed; Path=/CookieExample; Secure; HttpOnly'
]
my data=3 cookie isn't showing, and my response is still access denied, (the JSON response if data = 3 cookie doesn't exist) IS my cookie not registering? Or is the console log just executing before the cookie is added? any insight is helpful
request is fully deprecated, use axios for example.
All of the syntaxes are fully documented on the official npm library.
then:
const axios = require('axios')
axios.get(URL, {
withCredentials: true, // to able getting cookies from the server back
headers: {
// put cookies in here
Cookie: "cookie1=value; cookie2=value; cookie3=value;"
}
}).then(res =>{
// res.data is your data back
})