Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GET HTTP request payload

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.


like image 474
Sebastien Lorber Avatar asked May 03 '13 13:05

Sebastien Lorber


People also ask

Can a get request have a payload?

Note: Sending body/payload in a GET request may cause some existing implementations to reject the request — while not prohibited by the specification, the semantics are undefined. It is better to just avoid sending payloads in GET requests.

How do you find the payload of an HTTP request?

If the request contains HTTP multipart message (e.g. multiple attachments), you can access individual payloads using request:part:[part_name] in the reader file URL. Payload can also be accessed using the CTL function getRequestBody.

Can a HTTP GET request have a body?

GET requests don't have a request body, so all parameters must appear in the URL or in a header. While the HTTP standard doesn't define a limit for how long URLs or headers can be, mostHTTP clients and servers have a practical limit somewhere between 2 kB and 8 kB.

What is the payload of an HTTP request?

The HTTP message payload body is the information ("payload") part of the data that is sent in the HTTP Message Body (if any), prior to transfer encoding being applied. If transfer encoding is not used, the payload body and message body are the same!


3 Answers

Having the GET response vary based on the request body will break caching. Don't go there.

like image 175
Julian Reschke Avatar answered Oct 02 '22 07:10

Julian Reschke


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
like image 21
Darren Shewry Avatar answered Oct 02 '22 05:10

Darren Shewry


Google App Engine, a popular web framework, uses a special url fetch library, which does not support making HTTP GET requests with a payload. Accordingly, if you want your API to reach Google App Engine users, then I would not recommend requiring this behavior.

I've opened an issue regarding this with google.

like image 20
speedplane Avatar answered Oct 02 '22 06:10

speedplane