Node.js 请求库未设置所有 cookie

标签 node.js http cookies

我在 Node.js 中看到了这种奇怪的行为 request图书馆。服务器 (bigbasket.com) 发送的所有 cookie 都不会出现在 response.headers 或 cookie jar 中。不过 google.co.in 一切正常!

var request = require('request');

// Start the first request
request('http://bigbasket.com/', function (error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(response.headers);
    }
});

// Start the second request
request('http://www.google.co.in/', function (error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(response.headers);
    }
});

以上代码产生以下输出。

>>============
>> http://www.google.co.in/
>>============
{ date: 'Tue, 21 Oct 2014 11:15:28 GMT',
  expires: '-1',
  'cache-control': 'private, max-age=0',
  'content-type': 'text/html; charset=ISO-8859-1',
  'set-cookie': 
   [ 'PREF=ID=5d1b92fe341ae90c:FF=0:TM=1413890128:LM=1413890128:S=McPDJGR9MFLDQ-E5; expires=Thu, 20-Oct-2016 11:15:28 GMT; path=/; domain=.google.co.in',
     'NID=67=wafc6_zujxhEsNgPfIAb5QGvbHW5GK8QpWfZMVIIvZQiuQHeYe-lM_nD_rlEIS2ZtTdSWA6wcgrfwaBXxySHJWiPJtqc6UBgHX3SYbk2ovxAq-TpyYTpsb79C90O3ht_; expires=Wed, 22-Apr-2015 11:15:28 GMT; path=/; domain=.google.co.in; HttpOnly' ],
  p3p: 'CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."',
  server: 'gws',
  'x-xss-protection': '1; mode=block',
  'x-frame-options': 'SAMEORIGIN',
  'alternate-protocol': '80:quic,p=0.01',
  'transfer-encoding': 'chunked' }
>>============
>>============
>> http://bigbasket.com/
>>============
{ 'content-type': 'text/html; charset=utf-8',
  date: 'Tue, 21 Oct 2014 11:15:27 GMT',
  server: 'nginx/1.4.6 (Ubuntu)',
  'set-cookie': [ 'csrftoken=808333d4e04b650a53e9e56433b10386; expires=Tue, 20-Oct-2015 11:15:27 GMT; Max-Age=31449600; Path=/' ],
  vary: 'Accept-Encoding',
  'transfer-encoding': 'chunked',
  connection: 'keep-alive' }
>>============

curl 的输出显示 7 cookie 由 bigbasket.com 服务器发送,但只有 1 出现在上述 header 中倾倒。 google.co.in 发送 2 并且两者都出现在 header 中。

 ===============================================================================================================================
 bash $ curl -v http://www.google.co.in/ > /dev/null
 ===============================================================================================================================
* Hostname was NOT found in DNS cache
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 74.125.236.87...
* Connected to www.google.co.in (74.125.236.87) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.38.0
> Host: www.google.co.in
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Tue, 21 Oct 2014 10:07:22 GMT
< Expires: -1
< Cache-Control: private, max-age=0
< Content-Type: text/html; charset=ISO-8859-1
< Set-Cookie: PREF=ID=96fbe73447c47f15:FF=0:TM=1413886042:LM=1413886042:S=YtAcjeJChCMfvhoL; expires=Thu, 20-Oct-2016 10:07:22 GMT; path=/; domain=.google.co.in
< Set-Cookie: NID=67=hU3D52q3Heg7Fczrzc2xUEPVhQIsnYThGdstpZ1VGPmt80gYxTwX0dKjeQ13CL4vL2r_oJTA7U-si6Us70laG5Amgg1Ayv0ByAmz4ACkKRLK76qUajEbOw3h5IBwcObF; expires=Wed, 22-Apr-2015 10:07:22 GMT; path=/; domain=.google.co.in; HttpOnly
< P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
* Server gws is not blacklisted
< Server: gws
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
< Alternate-Protocol: 80:quic,p=0.01
< Transfer-Encoding: chunked
< 
{ [data not shown]
100 19019    0 19019    0     0  85796      0 --:--:-- --:--:-- --:--:-- 86058
* Connection #0 to host www.google.co.in left intact 
===============================================================================================================================

===============================================================================================================================
bash$ curl -v http://bigbasket.com/ > /dev/null
===============================================================================================================================
* Hostname was NOT found in DNS cache
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 122.248.250.187...
* Connected to bigbasket.com (122.248.250.187) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.38.0
> Host: bigbasket.com
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: text/html; charset=utf-8
< Date: Tue, 21 Oct 2014 10:07:32 GMT
* Server nginx/1.4.6 (Ubuntu) is not blacklisted
< Server: nginx/1.4.6 (Ubuntu)
< Set-Cookie: _bb_ftvid="MzEzMTM2MzAwOA==|ZA0HTkMCAU4qBkRVUFJSBBBSV18AT1hRURB3LjE="; Domain=.bigbasket.com; expires=Tue, 21-Oct-2014 12:07:32 GMT; Max-Age=7200; Path=/
< Set-Cookie: _bb_vid="MzEzMTM2MzAwOA=="; Domain=.bigbasket.com; expires=Mon, 16-Oct-2034 10:07:32 GMT; Max-Age=630720000; Path=/
< Set-Cookie: sessionid=06531cfd4d53a6e6646d09fe055b571a; Domain=.bigbasket.com; expires=Tue, 04-Nov-2014 10:07:32 GMT; Max-Age=1209600; Path=/
< Set-Cookie: _bb_tc=0; Domain=.bigbasket.com; expires=Wed, 21-Oct-2015 10:07:31 GMT; Max-Age=31536000; Path=/
< Set-Cookie: csrftoken=37adbe93f43608cb6fbfb609d8ad34a4; expires=Tue, 20-Oct-2015 10:07:32 GMT; Max-Age=31449600; Path=/
< Set-Cookie: _bb_rdt="MzE1MDAyNDY0Mw==.0"; Domain=.bigbasket.com; expires=Wed, 21-Oct-2015 10:07:31 GMT; Max-Age=31536000; Path=/
< Set-Cookie: _bb_rd=2; Domain=.bigbasket.com; expires=Wed, 21-Oct-2015 10:07:31 GMT; Max-Age=31536000; Path=/
< Vary: Accept-Encoding
< Vary: Cookie, Accept-Encoding
< transfer-encoding: chunked
< Connection: keep-alive
< 
{ [data not shown]
100  115k    0  115k    0     0  96407      0 --:--:--  0:00:01 --:--:-- 96449
* Connection #0 to host bigbasket.com left intact
===============================================================================================================================

工作代码在 Runnable

最佳答案

问题是该网站正在期待/嗅探 User-Agentrequest 默认情况下不会发送。

这将在响应中显示各种 Set-Cookie header :

request('http://bigbasket.com/', {
  headers: {
    'User-Agent': 'node.js'
  }
}, function (error, response, body) {
  console.dir(response.headers);
});

关于Node.js 请求库未设置所有 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26486021/

相关文章:

http - 如何从 AWS Lambda 函数返回错误集合/对象并映射到 AWS API 网关响应代码

python - 如何在 Pylons 中使用重定向设置 cookie

javascript - Mocha 测试在使用 promises 时超时并跳过代码,为什么?

node.js - 如何处理来自SQS的多条消息?

ruby-on-rails - 如何将 curl 中的 POST 请求转换为 ruby​​?

cookies - Jmeter 的 HTTP Cookie Manager 组件自动提取什么类型的 cookie?

session - Memcache 打开后 Magento session 消失

javascript switch/case 在读取文件之前中断

javascript - Express 中间件 : app. 使用和 app.all

RESTful API 设计区分具有相同 HTTP 方法的相同 URI 上的操作