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

发布于 2024-12-03 00:06:15 字数 505 浏览 1 评论 0原文

我正在编写一个 RESTful api,并且正在考虑用户创建密钥的过程。我有以下可能性:

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

我查看了 来自 Joyent 的 API,在所有 PUT 和 POST 请求中,它们在请求正文中传递了一些数据。这是预期的吗?在 PUT 和 POST 请求中不要求请求正文真的是错误的吗?

I'm writting a RESTful api, and at I'm thinking about the process of a user creating a key. I have the following possibilities:

  • GET request to /new/<keyname> - although it's very easy I think I won't use this, because I heard GET is for retrieving and/or listing information;
  • POST request to /<keyname> - This seemed to me easy and simple enough, but does not pass any data in the request body. Can I do it this way ? Is this weird ?
  • POST request to /keys passing in the request body "keyname=SomeKey" - Is this the correct way ?

I looked at this API from joyent and in all their PUT and POST requests they pass some data in the request body. Is this expected ? Is it really wrong not to require a request body in a PUT and POST request ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你是我的挚爱i 2024-12-10 00:06:15

我在 Http-WG 上问了这个问题。这是我得到的最准确的答案:

需要注意的是,没有正文的消息仍然可以携带表示,因为表示是正文(长度可以为零)和标头的组合。

参见:
http://tools.ietf.org/ html/draft-ietf-httpbis-p3-payload-11#section-4

https://lists.w3.org/Archives/Public/ ietf-http-wg/2010JulSep/0276.html

总之,POST 不需要正文。我希望同样的理由也适用于 PUT。

I asked this question on the Http-WG. This was the most precise answer I got:

It's important to note that a message without a body can still carry a representation, because a representation is the combination of the body (which can be zero-length) and headers.

See:
http://tools.ietf.org/html/draft-ietf-httpbis-p3-payload-11#section-4

https://lists.w3.org/Archives/Public/ietf-http-wg/2010JulSep/0276.html

In summary, POST does not require a body. I would expect the same justification can be applied to PUT.

哥,最终变帅啦 2024-12-10 00:06:15

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. ...

POST

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.

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

根据我的阅读,我相信 POST 和 PUT 都需要一个主体(我知道这是一个非规范的描述)。

在 REST 上下文中,POST 是创建,PUT 是更新。我可以想象创建一个空对象(也许是未来信息的占位符),但我不认为空更新有多大用处。

RFC2616 is the base RFC for HTTP 1.1

In the most general form, an HTTP message is this (note the optional body):

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

Reading further gives this:

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. ...

and

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.

Both POST and PUT include the phrase entity enclosed in the request.

Based on my reading, I believe that a body is desired (a non-normative description, I know) for both POST and PUT.

In the context of REST, POST is create and PUT is update. I can imagine creating an empty object (perhaps a placeholder for future information), but I don't imagine much use of an empty update.

赠我空喜 2024-12-10 00:06:15

这不是必需的。您可以发送不带正文的 POST/PUT 请求,而是使用查询字符串参数。但要小心,如果您的参数包含 HTTP 无效的字符,您将必须对它们进行编码。

例如,如果您需要将“hello world”发布到端点,则必须使其看起来像这样: http://api.com?param=hello%20world

It is not required. You can send a POST/PUT request without a body and instead use query string parameters. But be careful if your parameters contain characters that are not HTTP valid you will have to encode them.

For example if you need to POST 'hello world' to and end point you would have to make it look like this: http://api.com?param=hello%20world

日裸衫吸 2024-12-10 00:06:15

一句话回答你的问题。是的,正文中应该有正文/内容,但这不是必需的(强制)。

To answer your question in one line. Yes it is expected to have Body/Content in body, but it is not required(Mandatory).

总攻大人 2024-12-10 00:06:15

最好的方法可能是您的第三个选择:使用 keyname=SomeKey POST 到 /keys

原因如下:您可能希望向 API 添加另一个函数,例如 create_new_user。这样就很难区分尝试 POST 名为 create_new_user 的密钥的用户和尝试使用 create_new_user 函数的用户之间的区别。

您正确地说不应使用 GET 来执行此操作,因为 GET 操作"应该没有采取行动的意义
除了检索之外。”(RFC 2616)

Probably the best way is your third option: POST to /keys with keyname=SomeKey.

Here's why: You may wish to add another function to your API, for example create_new_user. It would then be difficult to tell the difference between a user trying to POST a key called create_new_user and a user trying to use the create_new_user function.

You are correct in saying that you should not be using GET to do this operation as the GET operation "SHOULD NOT have the significance of taking an action
other than retrieval." (RFC 2616)
.

世俗缘 2024-12-10 00:06:15

根据 okHttp3(android 的 HTTP 库):以下方法需要主体:POST、PUT、PATCH、PROPPATCH (WebDAV) 和 REPORT (源< /a>)。如果您尝试使用没有正文的给定方法执行请求,它甚至会崩溃。

According to okHttp3 (an HTTP library for android): the following methods need a body: POST, PUT, PATCH, PROPPATCH (WebDAV) and REPORT (source). It even crashes if you try to do a request with the given methods without a body.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文