简体   繁体   English

如何使用 NodeJs 请求 GET 模块发送 cookie?

[英]How can I send cookie using NodeJs request GET module?

I'm working on a project in NodeJs that requires me to login to get a cookie which would be used when retrieving data.我正在 NodeJs 中处理一个项目,该项目要求我登录以获取在检索数据时使用的 cookie。 I've got following code which succeeds to log in and return me a cookie with correct formatting:我有以下代码成功登录并返回一个格式正确的cookie:

 var request = require('request');

var requestData = {
  "username":"myUsername",
  "password":"myPassword"
  }

//request post request
request({
    url: 'http://localhost/login',
    method: "POST",
    json: requestData}
    ,function(err, res) {
        if(err){
            console.log("it did not work: " + err)
        }

            console.log(res.statusCode)//logs as 201 sucess
            console.log("heres the cookie: "+res.headers['set-cookie']) //returns cookie in correct format
            var cookie = res.headers['set-cookie']
            //requesting data
            request({
              url: 'http://localhost/delivery-stats',
              method: "GET",
              header: {
                'set-cookie': cookie
              }
            },function(err,response){
                console.log(response.headers) // one of the headers says user is not authorised

            }
            )

});

My problem is that when i try to do GET request with cookie attached to it it says that user is unauthorised, which means that the cookie was not passed correctly, anyone would know on how to do this using request module?我的问题是,当我尝试使用附加了 cookie 的 GET 请求时,它说用户未经授权,这意味着 cookie 没有正确传递,有人会知道如何使用请求模块执行此操作吗? Thanks谢谢

After a few hours I've found a solution, instead of :几个小时后,我找到了一个解决方案,而不是:

 //requesting data
        request({
          url: 'http://localhost/delivery-stats',
          method: "GET",
          header: {
            'set-cookie': cookie
          }

it had to be :它必须是:

 //requesting data
        request({
          url: 'http://localhost/delivery-stats',
          method: "GET",
          header: {
            'Cookie': cookie
          }

Because that is correct way to send cookies via request, but it was poorly documented so it took me some time to figure out.因为这是通过请求发送 cookie 的正确方法,但它的记录很差,所以我花了一些时间才弄清楚。 Hopefully this would help someone in the future.希望这会对将来的某人有所帮助。

For anyone stumbling across this in 2019+, the correct way to add a cookie to a request is using the cookie jar property setCookie.对于在 2019 年以上遇到此问题的任何人,将 cookie 添加到请求的正确方法是使用 cookie jar 属性 setCookie。 request's cookie jar is based on tough-cookie, and you can reference that package's documentation for more detail. request 的cookie jar 基于tough-cookie,您可以参考该包的文档以获取更多详细信息。

// Require the request     
const request = require('request');

// Setup the cookie jar to carry cookies
const cj = request.jar();

// Add your cookie to the jar (URL is parsed into cookie parts)
cj.setCookie(stateCookie, 'https://yoursite.com/');

// Send your request and include the cookie jar
request(
    'https://request-site.com/api/things', 
    { jar: cj },
    (error, response, body)=>{
        // do things
    }
);

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

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