简体   繁体   English

从PhantomJS内部发送时,不传输HTTP Content-type标头和正文

[英]HTTP Content-type header and body are not transmitted when sent from within PhantomJS

When initiating an HTTP DELETE request with content-type header and body, these two elements are: 使用内容类型标头和正文启动HTTP DELETE请求时,这两个元素是:

  • Well received when the request is initiated from within Chrome, 从Chrome内发起请求后,收到好评,
  • Not received when the request is initiated from within PhantomJS. 从PhantomJS内部发起请求时未收到。

Which of the two is behaving as per the standard? 按照标准,这两个行为中的哪个?

It is to be noted that in both cases, the client and the server are the same. 要注意的是,在两种情况下,客户端和服务器都是相同的。

Below are the logs of the request at the server level. 以下是服务器级别的请求日志。

Log of the request sent from within PhantomJS 从PhantomJS内部发送的请求的日志

SERVER: -------------------------------------------------------
SERVER: - R E Q U E S T                                       -
SERVER: -------------------------------------------------------
SERVER: req.url      /test/del
SERVER: req.method   OPTIONS
SERVER: req.headers  { 'access-control-request-method': 'DELETE',
  origin: 'http://localhost:9876',
  'user-agent': 'Mozilla/5.0 (Unknown; Linux x86_64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.7 Safari/534.34',
  referer: 'http://localhost:9876/context.html',
  'access-control-request-headers': 'Content-Type, Accept',
  accept: '*/*',
  connection: 'Keep-Alive',
  'accept-encoding': 'gzip',
  'accept-language': 'fr-FR,en,*',
  host: 'localhost:9009' }
SERVER: req.query    {}
SERVER: req.body     {}
SERVER: -------------------------------------------------------
SERVER: -------------------------------------------------------
SERVER: - R E S P O N S E                                     -
SERVER: -------------------------------------------------------
SERVER: res.headers  { 'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
  'Access-Control-Allow-Methods': 'PUT, DELETE',
  'Access-Control-Allow-Origin': '*' }
SERVER: -------------------------------------------------------
127.0.0.1 - - [Thu, 13 Nov 2014 16:07:08 GMT] "OPTIONS /test/del HTTP/1.1" 200 - "http://localhost:9876/context.html" "Mozilla/5.0 (Unknown; Linux x86_64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.7 Safari/534.34"
SERVER: -------------------------------------------------------
SERVER: - R E Q U E S T                                       -
SERVER: -------------------------------------------------------
SERVER: req.url      /test/del
SERVER: req.method   DELETE
SERVER: req.headers  { origin: 'http://localhost:9876',
  'user-agent': 'Mozilla/5.0 (Unknown; Linux x86_64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.7 Safari/534.34',
  accept: 'application/json, application/json;q=0.8, text/plain;q=0.5, */*;q=0.2',
  referer: 'http://localhost:9876/context.html',
  connection: 'Keep-Alive',
  'accept-encoding': 'gzip',
  'accept-language': 'fr-FR,en,*',
  host: 'localhost:9009' }
SERVER: req.query    {}
SERVER: req.body     {}
SERVER: -------------------------------------------------------
SERVER: -------------------------------------------------------
SERVER: - R E Q U E S T                                       -
SERVER: -------------------------------------------------------
SERVER: req.url      /del
SERVER: req.method   DELETE
SERVER: req.headers  { origin: 'http://localhost:9876',
  'user-agent': 'Mozilla/5.0 (Unknown; Linux x86_64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.7 Safari/534.34',
  accept: 'application/json, application/json;q=0.8, text/plain;q=0.5, */*;q=0.2',
  referer: 'http://localhost:9876/context.html',
  connection: 'Keep-Alive',
  'accept-encoding': 'gzip',
  'accept-language': 'fr-FR,en,*',
  host: 'localhost:9009' }
SERVER: req.query    {}
SERVER: req.body     {}
SERVER: -------------------------------------------------------
SERVER: -------------------------------------------------------
SERVER: - R E S P O N S E                                     -
SERVER: -------------------------------------------------------
SERVER: res.headers  { 'Content-type': 'application/json',
  'Access-Control-Allow-Origin': '*' }
SERVER: res.body     { code: 'Declined',
  reason: 'UNEXPECTED CONTENT',
  message: 'The content-type "undefined" is unexpected. Please use "application/json".' }
SERVER: -------------------------------------------------------

Log of the request sent from within Chrome 从Chrome发送的请求的日志

SERVER: -------------------------------------------------------
SERVER: - R E Q U E S T                                       -
SERVER: -------------------------------------------------------
SERVER: req.url      /test/del
SERVER: req.method   OPTIONS
SERVER: req.headers  { host: 'localhost:9009',
  connection: 'keep-alive',
  'access-control-request-method': 'DELETE',
  origin: 'http://localhost:9876',
  'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36',
  'access-control-request-headers': 'accept, content-type',
  accept: '*/*',
  referer: 'http://localhost:9876/context.html',
  'accept-encoding': 'gzip,deflate,sdch',
  'accept-language': 'en-US,en;q=0.8' }
SERVER: req.query    {}
SERVER: req.body     {}
SERVER: -------------------------------------------------------
SERVER: -------------------------------------------------------
SERVER: - R E S P O N S E                                     -
SERVER: -------------------------------------------------------
SERVER: res.headers  { 'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
  'Access-Control-Allow-Methods': 'PUT, DELETE',
  'Access-Control-Allow-Origin': '*' }
SERVER: -------------------------------------------------------
127.0.0.1 - - [Thu, 13 Nov 2014 16:15:04 GMT] "OPTIONS /test/del HTTP/1.1" 200 - "http://localhost:9876/context.html" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36"
SERVER: -------------------------------------------------------
SERVER: - R E Q U E S T                                       -
SERVER: -------------------------------------------------------
SERVER: req.url      /test/del
SERVER: req.method   DELETE
SERVER: req.headers  { host: 'localhost:9009',
  connection: 'keep-alive',
  'content-length': '23',
  accept: 'application/json, application/json;q=0.8, text/plain;q=0.5, */*;q=0.2',
  origin: 'http://localhost:9876',
  'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36',
  'content-type': 'application/json',
  referer: 'http://localhost:9876/context.html',
  'accept-encoding': 'gzip,deflate,sdch',
  'accept-language': 'en-US,en;q=0.8' }
SERVER: req.query    {}
SERVER: req.body     { field1: 1, field2: 2 }
SERVER: -------------------------------------------------------
SERVER: -------------------------------------------------------
SERVER: - R E Q U E S T                                       -
SERVER: -------------------------------------------------------
SERVER: req.url      /del
SERVER: req.method   DELETE
SERVER: req.headers  { host: 'localhost:9009',
  connection: 'keep-alive',
  'content-length': '23',
  accept: 'application/json, application/json;q=0.8, text/plain;q=0.5, */*;q=0.2',
  origin: 'http://localhost:9876',
  'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36',
  'content-type': 'application/json',
  referer: 'http://localhost:9876/context.html',
  'accept-encoding': 'gzip,deflate,sdch',
  'accept-language': 'en-US,en;q=0.8' }
SERVER: req.query    {}
SERVER: req.body     { field1: 1, field2: 2 }
SERVER: -------------------------------------------------------
SERVER: -------------------------------------------------------
SERVER: - R E S P O N S E                                     -
SERVER: -------------------------------------------------------
SERVER: res.headers  { 'Content-type': 'application/json',
  'Access-Control-Allow-Origin': '*' }
SERVER: res.body     { code: 'Accepted' }
SERVER: -------------------------------------------------------

The RFC 2616 for HTTP/1.1 specifies the DELETE method in chapter 9.7. HTTP / 1.1RFC 2616在9.7章中指定了DELETE方法。 It does not specify anything about the enclosed entity (message body) in the description text which it does for POST and PUT. 没有指定有关封装的实体 (消息体),其中它对于POST和PUT说明文字东西。

From your logs it is apparent that PhantomJS doesn't even send the message body and therefore doesn't need to include the content type, because it assumes that it will never be used anyway as it is not defined. 从您的日志中可以明显看出,PhantomJS甚至不发送消息正文,因此不需要包括内容类型,因为它假定由于未定义它永远不会被使用。 It seems that Chrome sends the message body regardless. Chrome似乎会发送邮件正文。

There is a draft for an update which includes the following text in chapter 6.7: 一份更新草案,其中包括第6.7章中的以下文本:

Bodies on DELETE requests have no defined semantics. DELETE请求上的实体没有定义的语义。 Note that sending a body on a DELETE request might cause some existing implementations to reject the request. 请注意,在DELETE请求上发送正文可能会导致某些现有实现拒绝该请求。

Since PhantomJS 1.x is based on a more than three year old version of WebKit (pre-draft), it behaves this way. 由于PhantomJS 1.x基于三年以上的WebKit版本(预草稿),因此它的行为是这样的。 Chrome on the other hand may have implemented the proposed draft and actively sends the body on DELETE requests. 另一方面,Chrome可能已实现了建议的草案,并根据DELETE请求主动发送了正文。 See this question for more information. 有关更多信息,请参见此问题

If your operation depends on the message body for the DELETE method, you should change the implementation so that the resource which you delete is completely defined by the URI. 如果您的操作取决于DELETE方法的消息正文,则应更改实现,以便要删除的资源完全由URI定义。

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

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