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

时间:2014-10-21 11:52:21

标签: node.js http cookies

我在Node.js request库中看到了这种奇怪的行为。服务器发送的所有cookie(bigbasket.com)都不在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服务器发送bigbasket.com个cookie,但上述标头转储中只有1google.co.in发送2,两者都出现在标题中。

 ===============================================================================================================================
 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

的工作代码

1 个答案:

答案 0 :(得分:1)

问题是该网站期待/嗅探User-Agent,而request默认不发送。{/ p>

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

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