Search code examples
javascriptnode.jsexpresshttp

Using body in GET requests


Why are URL query strings superior to values in the body?


There are some obvious differences between the two ways to request data - like being able to see the parameters in the URL, or the fact that the requests can be saved in the browser's history.

But is there anything more to it?

Is it bad to request data with body?


Solution

  • There's nothing wrong with using a body. GET method does not have a body not because of some prejudice against the usage of body.

    If you have a look at the HTTP protocol specification, you might have some insight:

    "The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI."

    In simpler words, GET method is created to retrieve some resource using only URI. In even simpler words - if you can get some resource using only URI - GET is a method you should use.

    As a matter of fact, you can send body with GET request, but it will be just ignored (more information here: HTTP GET with request body)

    If you need to pass a body for whatever reason you will need to use a different method. POST is a prominent example:

    "The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line."

    If you are working on REST API's I would highly encourage to read HTTP specification https://www.rfc-editor.org/rfc/rfc2616