GET HTTP 请求负载 [英] GET HTTP request payload

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

问题描述

我正在设计一个 API,我想知道是否可以在 GET 请求上发送 JSON 有效负载?

I am designing an API and I wonder if it is fine to send a JSON payload on a GET request?

在另一个问题HTTP请求方法的有效载荷中,我们可以根据此链接:

In this other question Payloads of HTTP Request Methods, we can find according to this link:

  • HEAD - 没有定义的正文语义.
  • GET - 没有定义的正文语义.
  • PUT - 支持正文.
  • POST - 支持正文.
  • DELETE - 没有定义的正文语义.
  • TRACE - 不支持正文.
  • OPTIONS - 支持正文,但没有语义(可能在未来).

这是否意味着我不应该发送带有负载的 GET 请求?这样做有风险吗?

  • 比如某些 HTTP 客户端库无法发送此类负载?
  • 或者我的 Java API 代码不能在某些应用服务器上移植?
  • 还有什么吗?

我发现 ElasticSearch 在 GET 请求中使用了这样的负载:

I found out that ElasticSearch was using such a payload on a GET request:

$ curl -XGET 'http://localhost:9200/twitter/tweet/_search?routing=kimchy' -d '{
    "query": {
        "filtered" : {
            "query" : {
                "query_string" : {
                    "query" : "some query string here"
                }
            },
            "filter" : {
                "term" : { "user" : "kimchy" }
            }
        }
    }
}
'

所以如果这个流行的图书馆做到了而且没有人抱怨,那么也许我也可以这样做?

So if this popular libary does it and nobody complains, then perhaps I can do the same?

顺便说一句,我想知道混合 queryString 参数和 JSON 负载是否可以? 就像这个 ElasticSearch 查询一样.如果是这样,是否有规则可以让我们知道哪些参数应该是 queryString 参数或有效载荷参数?

By the way, I would like to know if this is OK to mix queryString parameters and JSON payload? Exactly like this ElasticSearch query does. If so, are there rules so that we know which arguments should be queryString parameters, or payload parameters?

这里我们可以读到:带有请求正文的 HTTP GET

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

Roy Fielding's comment about including a body with a GET request.

是的.换句话说,任何 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).

....罗伊

然后我真的不明白为什么它从来没有用,因为在我看来,将复杂的查询发送到不适合 queryParam 或 matrixParam 的服务器是有意义的.我认为 ElasticSearch API 设计者也有同样的想法...

Then I don't really understand why it is never useful, because it makes sense in my opinion to send complex queries to the server that wouldn't fit well on queryParam or matrixParam. I think ElasticSearch API designers think the same...

我打算设计一个可以这样调用的 API:

I am planning to design an API which can be called like that:

curl -XGET 'http://localhost:9000/documents/inbox?pageIndex=0&pageSize=10&sort=title'

curl -XGET 'http://localhost:9000/documents/trash?pageIndex=0&pageSize=10&sort=title'

curl -XGET 'http://localhost:9000/documents/search?pageIndex=0&pageSize=10&sort=title' -d '{
    "someSearchFilter1":"filterValue1",
    "someSearchFilter2":"filterValue2",
    "someSearchFilterList": ["filterValue3","xxx"]
    ... a lot more ...
}
'

您觉得还好吗?基于以上考虑.

推荐答案

另见:HTTP GET与请求正文 - 有关此的更多详细信息.

Also see: HTTP GET with request body - for more detail on this.

要点是:是的,您可以,但出于各种原因,您可能不应该这样做,包括:

The gist is: Yes you can, but you probably shouldn't for various reasons, including:

  • 您可能忽略了 HTTP 规范建议(您可能想要发布)
  • 您可能会引入缓存问题
  • 就 REST API 而言,这直观

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

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