16

I am maintaining a old .aspx page in which all the data required by the page to show the content is passed in the URL of the GET request as a part of query string. The result of this is that, as we keep on adding features, the URL keeps getting bigger.

I am thinking of shifting all the parameters from the query string to the Body of the GET request.

Is it a good design decision and what is the use case of a body in GET request?

πάντα ῥεῖ
  • 1,540
  • 3
  • 12
  • 19
kumarmo2
  • 269
  • 1
  • 2
  • 5
  • 2
    Why not simply using a `POST` request instead? – πάντα ῥεῖ Jun 17 '18 at 09:12
  • 3
    Because I am not creating or updating anything. I am just retrieving info from the server. – kumarmo2 Jun 17 '18 at 09:12
  • 1
    It's not unusual to use `POST` requests (e.g. using XML or json content) for simple queries as well. Interpreting such is easier done at the server site. – πάντα ῥεῖ Jun 17 '18 at 09:14
  • 9
    It's expected that GET requests are idempotent: requesting the same URL multiple times always gets you an equivalent result. This e.g. allows for caching (which some browsers and proxies do very aggressively). If you move query parameters into the request body, you are violating this expectation so please avoid this. You're setting yourself up for hard to debug problems. Unfortunately, some systems have an URL length limit on the order of a few KB. If you are still designing your API and find that you will deal with very complex queries, a GraphQL API might be a better fit than REST. – amon Jun 17 '18 at 12:01
  • 4
    In addition to what @amon said, putting a body in a GET request is explicitly in violation of the HTTP spec. Use POST. – Eric Stein Jun 17 '18 at 12:07
  • On the other hand, You will find useful the implementation of "alias" for common and frequent requests. There're cases where parameters are rather implicit than explicit. For example,`/resources/available-tickets` instead of `/resources/tickets/?available=true` – Laiv Jun 17 '18 at 13:48
  • 1
    @amon I am just wondering then why ``Elasticsearch`` uses a body in ``GEt`` Request? – kumarmo2 Jun 17 '18 at 14:45
  • 1
    @EricStein . Should'nt ``POST`` be used when you are ``Creating`` a resource? – kumarmo2 Jun 17 '18 at 14:47
  • 3
    @Manya The comments on [this Stack Overflow answer](https://stackoverflow.com/a/983458/1521179) discuss Elasticsearch a bit. Note that Elasticsearch is not typically consumed directly by browsers but only by internal applications, so they have a bit more flexibility – you have full control over the client and the network. – amon Jun 17 '18 at 14:57
  • @amon. I am satisfied by your explaination(and the sources you provided) . Shall I close this question as you haven't given an answer and only comment? If you give an answer, I can mark as correct. – kumarmo2 Jun 17 '18 at 16:43
  • @Manya No. Post should be used when it's valid according to the spec and it helps you solve your problem. – Eric Stein Jun 18 '18 at 12:06

1 Answers1

15

GET requests with a body are supported in the HTML specs.

See the Stack Overflow question Is this statement correct? HTTP GET method always has no message body for a discussion.

However, it's unusual. You will surprise people with that solution. Simply switching to a POST is a better idea.

The only downside (upside?) is annoying RESTfulness acolytes.

Glorfindel
  • 3,137
  • 6
  • 25
  • 33
Ewan
  • 70,664
  • 5
  • 76
  • 161
  • 1
    there are technical downsides since many javascript libraries don't support GET with a body. For databases where you are performing a search, it makes logical sense to do it that way, but you wouldn't be able to make the call from a single page app to your ElasticSearch or SOLR instance (the only APIs where I saw this combo implemented). – Berin Loritsch Jun 18 '18 at 14:18
  • 2
    This question says the same thing: https://stackoverflow.com/questions/978061/http-get-with-request-body "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. " The actual answer has more details. – Jerry Jeremiah Aug 26 '20 at 05:09