简体   繁体   English

无法从标头获取set-cookie以响应NodeJS

[英]Can't get set-cookie from header in response with NodeJS

I have tried 3 differents http library in node 我在节点中尝试了3种不同的http库

  1. node request 节点请求
  2. node requestify 节点requestify
  3. node needle 结针

All of the 3 libraries can't get my set-cookie from my response 这3个库都无法从我的回复中获取我的set-cookie

Request 请求

15:32:52 - WARN  - now testing with request
{ 'x-powered-by': 'Express',
  'content-type': 'application/json; charset=utf-8',
  'content-length': '4591',
  date: 'Thu, 11 Dec 2014 14:32:52 GMT',
  connection: 'keep-alive' }

Needle

15:32:52 - WARN  - now testing with needle
{ 'x-powered-by': 'Express',
  'content-type': 'application/json; charset=utf-8',
  'content-length': '4591',
  date: 'Thu, 11 Dec 2014 14:32:52 GMT',
  connection: 'close' }

Requestify 请求

15:32:52 - WARN  - now testing with requestify
{ 'x-powered-by': 'Express',
  'content-type': 'application/json; charset=utf-8',
  'content-length': '4591',
  date: 'Thu, 11 Dec 2014 14:32:52 GMT',
  connection: 'keep-alive' }

Here's the result header with a wget 这是带有wget的结果标头

Connecting to localhost (localhost)|127.0.0.1|:8000... connected.
HTTP request sent, awaiting response...
  HTTP/1.1 200 OK
  X-Powered-By: Express
  Accept-Ranges: bytes
  Date: Thu, 11 Dec 2014 14:38:49 GMT
  Cache-Control: public, max-age=0
  Last-Modified: Thu, 11 Dec 2014 09:56:58 GMT
  ETag: W/"7c2-2140188116"
  Content-Type: text/html; charset=UTF-8
  Content-Length: 1986
  set-cookie: session=s%3AHI2alIkPOxVLXgJCymvAPbTZt1urIpEE.dBB7ZrpWjKAkwSALBVgbyJtRq9ZABMq9VJh%2F3XT6r8A; Path=/; Expires=Thu, 11 Dec 2014 15:08:49 GMT; HttpOnly
  Connection: keep-alive
Length: 1986 (1.9K) [text/html]

If this can help, here are my headers from my request 如果这可以帮助,这是我请求中的标题

var commonHeaders = {
    'Accept': '*/*',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'fr',
    'Cache-Control': 'no-cache',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Connection': 'keep-alive',
    'Pragma': 'no-cache',
    'User-Agent': 'request'
};

While firefox, chrome, and wget can get the full header, I am not able to do authentification with node. 虽然firefox,chrome和wget可以获取完整的标头,但我无法使用node进行身份验证。 Is it a probleme with Node ? Node有问题吗? Should I do that in Python ? 我应该在Python中这样做吗?

Edit : 编辑:

I have tried to remove the data posted through node, I also have the same response than with wget. 我试图删除通过节点发布的数据,我的响应也与使用wget的响应相同。 While I am using a browser and go through the login action, it return my json + set-cookie. 当我使用浏览器并执行登录操作时,它将返回我的json + set-cookie。

Why isn't that possible with node ? 为什么用node不可能呢?

You probably don't use the cookies correctly. 您可能没有正确使用Cookie。 Usually they are not accessed directly as header variables. 通常,它们不作为标头变量直接访问。 See request documentation for how to handle cookies, there are good examples there. 有关如何处理Cookie的信息,请参阅请求文档。那里有很好的示例。

Cookies are disabled by default (else, they would be used in subsequent requests). Cookies默认是禁用的(否则,它们将在后续请求中使用)。 To enable cookies, set jar to true (either in defaults or options) and install tough-cookie. 要启用cookie,请将jar设置为true(默认设置或选项),然后安装hard-cookie。

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

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