带有请求正文的 HTTP GET [英] HTTP GET with request body

查看:28
本文介绍了带有请求正文的 HTTP GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我们的应用程序开发一个新的 RESTful 网络服务.

I'm developing a new RESTful webservice for our application.

对某些实体执行 GET 时,客户端可以请求实体的内容.如果他们想添加一些参数(例如对列表进行排序),他们可以在查询字符串中添加这些参数.

When doing a GET on certain entities, clients can request the contents of the entity. If they want to add some parameters (for example sorting a list) they can add these parameters in the query string.

或者,我希望人们能够在请求正文中指定这些参数.HTTP/1.1 似乎没有明确禁止这一点.这将允许他们指定更多信息,可能更容易指定复杂的 XML 请求.

Alternatively I want people to be able to specify these parameters in the request body. HTTP/1.1 does not seem to explicitly forbid this. This will allow them to specify more information, might make it easier to specify complex XML requests.

我的问题:

  • 这完全是个好主意吗?
  • HTTP 客户端在 GET 请求中使用请求正文时会遇到问题吗?

https://www.rfc-editor.org/rfc/rfc2616

推荐答案

Roy Fielding 关于在 GET 请求中包含正文的评论.

是的.换句话说,任何 HTTP 请求消息都允许包含消息主体,因此必须考虑到这一点来解析消息.但是,GET 的服务器语义受到限制,因此主体(如果有)对请求没有语义意义.解析的需求和方法语义的需求是分开的.

Yes. In other words, any HTTP request message is allowed to contain a message body, and thus must parse messages with that in mind. Server semantics for GET, however, are restricted such that a body, if any, has no semantic meaning to the request. The requirements on parsing are separate from the requirements on method semantics.

所以,是的,您可以使用 GET 发送正文,但不,这样做永远没有用.

So, yes, you can send a body with GET, and no, it is never useful to do so.

这是 HTTP/1.1 分层设计的一部分,一旦规范被分区(正在进行中),它​​将再次变得清晰.

This is part of the layered design of HTTP/1.1 that will become clear again once the spec is partitioned (work in progress).

....罗伊

是的,您可以使用 GET 发送请求正文,但它应该没有任何意义.如果您通过在服务器上解析它并根据其内容更改您的响应来赋予它含义,那么您就忽略了 HTTP/1.1 规范,第 4.3 节:

Yes, you can send a request body with GET but it should not have any meaning. If you give it meaning by parsing it on the server and changing your response based on its contents, then you are ignoring this recommendation in the HTTP/1.1 spec, section 4.3:

...如果请求方法不包含实体主体的定义语义,则消息主体 应该在处理请求时被忽略.

...if the request method does not include defined semantics for an entity-body, then the message-body SHOULD be ignored when handling the request.

以及 HTTP/1.1 规范第 9.3 节中对 GET 方法的描述:

GET 方法意味着检索由 Request-URI 标识的任何信息 ([...]).

The GET method means retrieve whatever information ([...]) is identified by the Request-URI.

它指出请求正文不是 GET 请求中资源标识的一部分,只是请求 URI.

which states that the request-body is not part of the identification of the resource in a GET request, only the request URI.

更新

被称为HTTP/1.1 规范"的 RFC2616现在已经过时了.2014 年,它被 RFC 7230-7237 取代.引用在处理请求时应该忽略消息体"已被删除.现在只是请求消息框架与方法语义无关,即使该方法没有定义任何消息体的用途".第二个引用GET 方法意味着检索任何信息......由请求 URI 标识"被删除了.- 来自评论

The RFC2616 referenced as "HTTP/1.1 spec" is now obsolete. In 2014 it was replaced by RFCs 7230-7237. Quote "the message-body SHOULD be ignored when handling the request" has been deleted. It's now just "Request message framing is independent of method semantics, even if the method doesn't define any use for a message body" The 2nd quote "The GET method means retrieve whatever information ... is identified by the Request-URI" was deleted. - From a comment

来自 HTTP 1.1 2014 规范:

GET 请求消息中的有效载荷没有定义的语义;在 GET 请求上发送有效负载正文可能会导致某些现有实现拒绝该请求.

A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.

这篇关于带有请求正文的 HTTP GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆