PUT 和 POST 请求是否需要/期望有请求正文?
我正在编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我在 Http-WG 上问了这个问题。这是我得到的最准确的答案:
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:
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.
RFC2616 是 HTTP 1.1 的基础 RFC
在最常见的形式中,HTTP 消息是这样的(注意可选正文):
进一步阅读会得出:
POST
和 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):
Reading further gives this:
and
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.
这不是必需的。您可以发送不带正文的 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
一句话回答你的问题。是的,正文中应该有正文/内容,但这不是必需的(强制)。
To answer your question in one line. Yes it is expected to have Body/Content in body, but it is not required(Mandatory).
最好的方法可能是您的第三个选择:使用
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
withkeyname=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 calledcreate_new_user
and a user trying to use thecreate_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).
根据 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.