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

时间:2014-11-13 16:20:33

标签: http phantomjs content-type http-delete

当使用内容类型标题和正文启动HTTP DELETE请求时,这两个元素是:

  • 从Chrome中启动请求时收到的很好,
  • 从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: -------------------------------------------------------

记录从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: -------------------------------------------------------

1 个答案:

答案 0 :(得分:0)

RFC 2616 for HTTP/1.1指定第9.7节中的DELETE方法。 在描述文本中为POST和PUT指定封闭实体(消息正文)的任何内容。

从您的日志中可以看出,PhantomJS甚至不发送消息正文,因此不需要包含内容类型,因为它假定它永远不会被使用,因为它没有被定义。似乎Chrome无论如何都会发送邮件正文。

有一个draft for an update,其中包含第6.7章中的以下文字:

  

DELETE请求上的实体没有定义的语义。注意      在DELETE请求上发送正文可能会导致一些存在      拒绝请求的实现。

由于PhantomJS 1.x基于超过三年的WebKit版本(草稿前),因此它的行为方式如此。另一方面,Chrome可能已经实施了拟议的草案,并主动发送有关DELETE请求的正文。有关详细信息,请参阅this question

如果您的操作依赖于DELETE方法的消息正文,则应更改实现,以便 delete 的资源完全由URI定义。