簡體   English   中英

從PhantomJS內部發送時,不傳輸HTTP Content-type標頭和正文

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

使用內容類型標頭和正文啟動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: -------------------------------------------------------

HTTP / 1.1RFC 2616在9.7章中指定了DELETE方法。 沒有指定有關封裝的實體 (消息體),其中它對於POST和PUT說明文字東西。

從您的日志中可以明顯看出,PhantomJS甚至不發送消息正文,因此不需要包括內容類型,因為它假定由於未定義它永遠不會被使用。 Chrome似乎會發送郵件正文。

一份更新草案,其中包括第6.7章中的以下文本:

DELETE請求上的實體沒有定義的語義。 請注意,在DELETE請求上發送正文可能會導致某些現有實現拒絕該請求。

由於PhantomJS 1.x基於三年以上的WebKit版本(預草稿),因此它的行為是這樣的。 另一方面,Chrome可能已實現了建議的草案,並根據DELETE請求主動發送了正文。 有關更多信息,請參見此問題

如果您的操作取決於DELETE方法的消息正文,則應更改實現,以便要刪除的資源完全由URI定義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM