How to send post request with cookies.

Posted by benben3963 at 9:17 AM on 5/8/2019 EST:

I need to deliver to the cookie monster some delicious cookies with node js. How would I do this. I have googled and seen tons of examples but they don't work. Please use default https module if you can.

˄ 0  ˅ 0

Comments:
ModernFeelGames
var https = require("https");

var opts = {
hostname: "host name here",
path: "path here",
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Cookie': 'CookieName=cookieValue',
method: "POST"
}

var req = https.request(opts, function(res) {
var data = "";

res.on('data', function(body) {
data = data+body;
});

res.on('end', function() {
//When Node.js receives a response...
});
});

req.write('stuff');
req.end();

ModernFeelGames
so you just include 'Cookie': 'CookieName=CookieValue', in the headers.

ModernFeelGames
Also, if you want to add another cookie, just duplicate the Cookie header and stuffs.

benben3963
What is the write('stuff')

ModernFeelGames
@benben3963 Thats the post data, so like if you wanted to post to stibarc using herronjo's web api, if you wanted to post to stibarc, it would be (assuming you have the host and path set up) req.write(`sess=${sess}&title=${title}&content=${content}`)

so just the stuff like that
Log in to post a comment!