27

I'm kind of a newbie in NodeJs. I'm trying to make an http request and pass a cookie. I've read all the threads on stackoverflow about it and made up a piece of code that should theoretically work. But, it doesn't.

What I'm trying to do is to send a cookie with the store code to one online-shop which will show me the information about this very shop. If there is no cookie it shows the default div asking to choose a shop.

Here is my code:

var request = require('request'), http = require('follow-redirects').http, request = request.defaults({ jar: true }); var cookieString = 'MC_STORE_ID=66860; expires=' + new Date(new Date().getTime() + 86409000); var str = ''; //var uri = 'example.de'; //var j = request.jar(); var cookie = request.cookie(cookieString); j.setCookie(cookie, uri); var options = { hostname: 'example.de', path: '/pathexample', method: 'GET', headers: { 'User-Agent': 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11', 'Cookie': cookie, 'Accept': '/', 'Connection': 'keep-alive' } //,jar: j }; http.request(options, function (resp) { resp.setEncoding('utf8'); console.log(resp.headers); if (resp.statusCode) { resp.on('data', function (part) { str += part; }); resp.on('end', function (part) { console.log(str); }); resp.on('error', function (e) { console.log('Problem with request: ' + e.message); }); } }).end(str); 

I assume that the cookie will be sent and accepted with my request, but it isn't. I've also tried jar. I commented it out in the code. But, it seems not to work for me either. When I do console.log(resp.headers) I see the original cookies, but not mine. Can someone give me a hint?

The cookie structure is correct. When I run document.cookie=cookie; in google chrome console it is succsessfuly replaced.

2
  • 2
    What is request module ? github.com/mikeal/request ? Commented Jun 23, 2014 at 12:18
  • 1
    yes, exactly this one Commented Jun 23, 2014 at 12:23

5 Answers 5

19

In your headers try to put directly the cookieString

headers: { 'User-Agent': 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11', 'Cookie': cookieString, 'Accept': '/', 'Connection': 'keep-alive' } 
Sign up to request clarification or add additional context in comments.

1 Comment

For some reason "Cookie" header works perfectly but "cookie" option seems not work in request nor jsdom. Strange.
17

I had the same issue, @Ricardo helped me to figure out. I have removed the jar key from header

//Set the cookie instead of setting into header var cookie = request.cookie('MC_STORE_ID=66860'); // Set the headers for the request var headers = { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(post_data), 'Cookie': cookie }; // Configure the request var options = { url: requestObj["URL"], method: 'POST', headers: headers }; // Start the request request(options, function (error, response, body) { if (!error && response.statusCode === 200) { // Print out the response body cb(null, {status: 0, statusDsc: "success", data: body}); } else { cb(error, {status: 2, statusDsc: JSON.stringify(error)}); } }); 

This is kind of mix between your cookie data and my request function.

1 Comment

Thanks, this worked for me. I didn't realize you have to do var cookie = request.cookie('MC_STORE_ID=66860');. I was just doing var headers = { 'Cookie': rawCookieStr };
4

Your variable cookieString is a Set-Cookie header. It's a server header that the server sends to the client but not a cookie that the client sends to the server.

Set-Cookie header :

 MC_STORE_ID=66860; expires=1234; 

The client sends in Cookie Header this:

 MC_STORE_ID=66860; 

Try these functions :

function dataCookieToString(dataCookie) { var t = ""; for (var x = 0; x < dataCookie.length; x++) { t += ((t != "") ? "; " : "") + dataCookie[x].key + "=" + dataCookie[x].value; } return t; } function mkdataCookie(cookie) { var t, j; cookie = cookie.toString().replace(/,([^ ])/g, ",[12],$1").split(",[12],"); for (var x = 0; x < cookie.length; x++) { cookie[x] = cookie[x].split("; "); j = cookie[x][0].split("="); t = { key: j[0], value: j[1] }; for (var i = 1; i < cookie[x].length; i++) { j = cookie[x][i].split("="); t[j[0]] = j[1]; } cookie[x] = t; } return cookie; } 

To start use this :

dataCookie = mkdataCookie('MC_STORE_ID=66860; expires=' + new Date(new Date().getTime() + 86409000)); // or mkdataCookie(resp.headers["set-cookie"]) in your `http.request.end()` function. 

mkdataCookie returns an object.

Then you can set it in your header:

headers: { "User-Agent": "NodeJS/1.0", "Cookie": dataCookieToString(dataCookie) } 

2 Comments

Thank you for your answer! Now I understand what I was doing wrong. Just one more question: data.length in the header suppose to be dataCookieToString(dataCookie).length or what is "data"? I was trying to set it like this but the script is just hänging without a response.
Sorry bad copy/past. (Content-Length, is length of data that you send with the POST request).
3

Unfortunately both answers didn't work for me. I will repeat that the aim was to make a http request with passing a cookie containing the store code. To get the pages HTML for this very branch, not the standars page asking me to pick one. After trying both options I didn't get any error, but also it didn't pass the cookie to the website. But thanks a lot to Subject for providing me the functions. It gave me an idea to use those and the offered header with a JSDOM. So here is the code which worked for me:

var jsdom = require('jsdom'); var stores = ["68357"]; function dataCookieToString(dataCookie) { var t = ""; for (var x = 0; x < dataCookie.length; x++) { t += ((t !== "") ? "; " : "") + dataCookie[x].key + "=" + dataCookie[x].value; } return t; } function mkdataCookie(cookie) { var t, j; cookie = cookie.toString().replace(/,([^ ])/g, ",[12],$1").split(",[12],"); for (var x = 0; x < cookie.length; x++) { cookie[x] = cookie[x].split("; "); j = cookie[x][0].split("="); t = { key: j[0], value: j[1] }; for (var i = 1; i < cookie[x].length; i++) { j = cookie[x][i].split("="); t[j[0]] = j[1]; } cookie[x] = t; } return cookie; } var dataCookie = mkdataCookie('MC_STORE_ID=' + stores[0] + '; Expires=' + new Date(new Date().getTime() + 86409000)); jsdom.env({ url: 'http://www.example.de', headers: { 'User-Agent': "NodeJS/1.0", 'Cookie': dataCookieToString(dataCookie) }, scripts: ['http://code.jquery.com/jquery-1.5.min.js'], done: function (err, window) { var $ = window.jQuery; console.log($('body').html()); } }); 

The cookie was set succsessfuly and I got the webpages source code for this very branch I wanted. Thank you for the answers and hope it would help someone else.

1 Comment

'Both answers' is an ephemeral statement. Here in 2020 it takes quite a bit of work to discover which two answers you were referring to.
1

To convert from array of cookies (coming from set-cookie header from response) to a request cookie:

 headers: { 'cookie':cookies.map(c=>c.substr(0, c.indexOf(";"))).reduce((a, b) => a + "; " + b) } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.