How to send post request with cookies.

benben3963 Verified
05/08/19, 2:17 PM
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.

Upvotes0 Downvotes0 Link

5 Comments


ModernFeelGames Verified
05/08/19, 2:17 PM
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();

Upvotes0 Downvotes0 Link
ModernFeelGames Verified
05/08/19, 2:17 PM
so you just include 'Cookie': 'CookieName=CookieValue', in the headers.

Upvotes0 Downvotes0 Link
ModernFeelGames Verified
05/08/19, 2:17 PM
Also, if you want to add another cookie, just duplicate the Cookie header and stuffs.

Upvotes0 Downvotes0 Link
benben3963 Verified
05/08/19, 2:17 PM
What is the write('stuff')

Upvotes0 Downvotes0 Link
ModernFeelGames Verified
05/08/19, 2:17 PM
@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

Upvotes0 Downvotes0 Link