Larry Lu
Larry Lu

Reputation: 1661

To post data with cookie using node.js

I post some data to a url.
This is my code.

request = require("request");

var url = "http://www.cjihrig.com/development/php/hello_cookies.php";
var jar = request.jar();
var cookie = request.cookie("name=Larry");
jar.setCookie(cookie, url);

var req = {
    url: url,
    jar: jar,
};

request(req, function(err, res, body){
    if(err) console.log(err);

    console.log(body);
});

Then I get "Hello Larry!".
When I post again, I also get "Hello Larry!".

But when I use Google Chrome to visit this website,
I get "Hello !" in the first time and "Hello Stranger!" in the second time.
I think that's because the cookie is modified by the website.
Is there any way to get the modified cookie in node.js?

Upvotes: 0

Views: 326

Answers (1)

user6101582
user6101582

Reputation:

Taken from https://github.com/request/request:

To inspect your cookie jar after a request:

var j = request.jar()
request({url: 'http://www.google.com', jar: j}, function () {
  var cookie_string = j.getCookieString(url); // "key1=value1; key2=value2; ..."
  var cookies = j.getCookies(url);
  // [{key: 'key1', value: 'value1', domain: "www.google.com", ...}, ...]
})

Upvotes: 1

Related Questions