PUT和POST请求是否需要/预期有请求正文?

Joã*_*imo 24 rest post http put httpwebrequest

我正在写一个RESTful api,而我正在考虑用户创建密钥的过程.我有以下几种可能性:

  • GET请求/new/<keyname> - 虽然这很容易,但我认为我不会使用它,因为我听说GET用于检索和/或列出信息;
  • POST请求/<keyname> - 这在我看来简单易行,但不会在请求正文中传递任何数据.我可以这样做吗?这有点奇怪吗?
  • POST请求/keys传递请求正文"keyname=SomeKey" - 这是正确的方法吗?

从joyent查看了这个API,在他们所有的PUT和POST请求中,他们在请求体中传递了一些数据.这是预期的吗?在PUT和POST请求中不要求请求正文是否真的错了?

Dar*_*ler 15

我在Http-WG问了这个问题.这是我得到的最精确的答案http://lists.w3.org/Archives/Public/ietf-http-wg/2010JulSep/0276.html

总之,POST不需要正文.我希望同样的理由可以应用于PUT.

  • POST确实需要一个正文,但该正文可以是一个空文档.差异是微妙的,但它不是一回事.例如,您仍然有空文档的mimetype. (7认同)
  • @PedroWerneck 您能否提供该断言的参考?我所读到的内容与这种观点不一致。 (2认同)
  • @PedroWerneck ...但我们在谈论 HTTP,不是吗?在您的理解中,如何将没有正文的 HTTP 表示更改为具有空正文的 HTTP 表示? (2认同)
  • @PedroWerneck我知道空的json文档与no-body不同,与空文本/纯文档不同.但这与原始问题无关.我仍然认为POST是无效的.这就是你刚才在上次评论中所说的内容. (2认同)

DwB*_*DwB 6

RFC2616是HTTP 1.1的基本RFC

在最常见的形式中,HTTP消息是这样的(注意可选主体):

generic-message = start-line
                  *(message-header CRLF)
                  CRLF
                  [ message-body ]
start-line      = Request-Line | Status-Line

阅读进一步说明:

9.5 POST

   The POST method is used to request that the origin server accept the
   entity enclosed in the request as a new subordinate of the resource
   identified by the Request-URI in the Request-Line. ...

9.6 PUT

   The PUT method requests that the enclosed entity be stored under the
   supplied Request-URI. ...

   The fundamental difference between the POST and PUT requests is
   reflected in the different meaning of the Request-URI. The URI in a
   POST request identifies the resource that will handle the enclosed
   entity. That resource might be a data-accepting process, a gateway to
   some other protocol, or a separate entity that accepts annotations.
   In contrast, the URI in a PUT request identifies the entity enclosed
   with the request -- the user agent knows what URI is intended and the
   server MUST NOT attempt to apply the request to some other resource.

POST和PUT都包含请求中包含的短语实体.

根据我的阅读,我相信POST和PUT都需要一个正文(我知道非规范的描述).

在REST的上下文中,POST是创建的,PUT是更新的.我可以想象创建一个空对象(可能是未来信息的占位符),但我没想到会有太多的空更新.

  • 你在"REST的背景下"是什么意思?REST在哪里重新定义了HTTP POST方法的含义? (6认同)
  • POST不一定是创建请求."创建子坐标资源"只是建议的含义之一.关于POST的所有http规范都说它是不安全且非幂等的.其余语义未指定. (6认同)