简体   繁体   English

使用node.js通过cookie发布数据

[英]To post data with cookie using node.js

I post some data to a url. 我将一些数据发布到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!". 当我再次发帖时,我还会收到“ Hello Larry!”。

But when I use Google Chrome to visit this website, 但是,当我使用Google Chrome浏览器访问此网站时,
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. 我认为这是因为Cookie是由网站修改的。
Is there any way to get the modified cookie in node.js? 有什么方法可以在node.js中获取修改后的cookie吗?

Taken from https://github.com/request/request : 取自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", ...}, ...]
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM