GET HTTP请求有效载荷 [英] GET HTTP request payload

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

问题描述

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



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




  • HEAD - 没有定义的身体语义。

  • GET - 没有定义的身体语义。

  • PUT - 身体支持

  • POST - Body supported。

  • DELETE - 没有定义的身体语义。

  • TRACE - 不支持身体。

  • 选项 - 身体受支持,但没有语义(可能在将来)。



这是否意味着我不应该发送具有有效载荷的GET请求?
有风险吗?就像有一些HTTP客户端库不能发送这样的有效载荷一样?






  • $ / $>




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

      $ curl -XGET 'http:// localhost:9200 / twitter / tweet / _search?routing = kimchy'-d'{
    query:{
    filtered:{
    query
    query_string:{
    查询:某些查询字符串
    }
    },
    过滤器:{
    :{user:kimchy}
    }
    }
    }
    }
    '

    所以如果这个流行的libary没有这样做,没有人抱怨,那么也许我可以这样做?



    方式,我想知道是否这可以混合使用queryString参数和JSON有效负载?完全像ElasticSearch查询一样。如果是这样,那么我们知道哪些参数应该是queryString参数或有效载荷参数?






    这里我们可以阅读:
    请求身份的HTTP GET


    Roy Fielding的评论是关于包含身份的GET请求。



    是的。换句话说,任何HTTP请求消息都被允许包含
    消息体,因此必须解决消息。服务器
    GET的语义,但是受到限制,使得一个正文(如果有的话)
    对该请求没有语义意义。解析
    的要求与方法语义的要求是分开的。



    所以,是的,你可以发送一个具有GET的身体,不,它永远不会有用的
    这样做。



    这是HTTP / 1.1的分层设计的一部分,一旦规范被分区,将再次清除
    正在进行中)



    .... Roy


    然后我真的不明白为什么它永远不会有用,因为我觉得将复杂的查询发送到服务器上,这不适合在queryParam或matrixParam上。
    我认为ElasticSearch API设计者认为相同...






    我正在计划设计一个可以被称为:

      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]
    ...更多...
    }
    '

    您看起来好吗?根据上述注意事项。



    解决方案

    另请参阅:具有请求体的HTTP GET - 关于这个的更多细节。



    主旨是:是的,但你可能不应该出于各种原因,包括:




    • 您可能会忽略HTTP规范建议(您可能想要POST)

    • 您可能会引入缓存问题

    • 这是直到REST API去


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

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

    • HEAD - No defined body semantics.
    • GET - No defined body semantics.
    • PUT - Body supported.
    • POST - Body supported.
    • DELETE - No defined body semantics.
    • TRACE - Body not supported.
    • OPTIONS - Body supported but no semantics (maybe in the future).

    Does this mean that I should not send a GET request with a payload? Are there risks to do so?

    • Like having some HTTP client libraries incapable of sending such a payload?
    • Or my Java API code not being portable on certain application servers?
    • Anything else?

    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?

    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?


    Here we can read: HTTP GET with request body

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

    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.

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

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

    ....Roy

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


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

    Does it seem fine to you? Based on the above considerations.


    解决方案

    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:

    • You're likely ignoring HTTP spec recommendations (you probably want to POST)
    • You may introduce caching issues
    • This is not intuitive as far as REST APIs go

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

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