278

A REST API can have arguments in several places:

  1. In the request body - As part of a json body, or other MIME type
  2. In the query string - e.g. /api/resource?p1=v1&p2=v2
  3. As part of the URL-path - e.g. /api/resource/v1/v2

What are the best practices and considerations of choosing between 1 and 2 above?
2 vs 3 is covered here.

2

6 Answers 6

118

What are the best practices and considerations of choosing between 1 and 2 above?

Usually the content body is used for the data that is to be uploaded/downloaded to/from the server and the query parameters are used to specify the exact data requested. For example when you upload a file you specify the name, mime type, etc. in the body but when you fetch list of files you can use the query parameters to filter the list by some property of the files. In general, the query parameters are property of the query not the data.

Of course this is not a strict rule - you can implement it in whatever way you find more appropriate/working for you.

You might also want to check the wikipedia article about query string, especially the first two paragraphs.

2
  • 4
    A reasonable takeaway to your above analysis is that idempotent operations are best kept in the url query strings and CRUD is best kept to strictly typed response bodies, which essentially takes advantage of SOP and prevents very basic forms of social engineering/phishing attacks
    – Rice
    Aug 19, 2019 at 22:03
  • 3
    @Rice R in CRUD is an indempotent operation.
    – user398039
    Aug 12, 2020 at 15:26
59

I'll assume you are talking about POST/PUT requests. Semantically the request body should contain the data you are posting or patching.

The query string, as part of the URL (a URI), it's there to identify which resource you are posting or patching.

You asked for a best practices, following semantics are mine. Of course using your rules of thumb should work, specially if the web framework you use abstract this into parameters.

You most know:

  • Some web servers have limits on the length of the URI.
  • You can send parameters inside the request body with CURL.
  • Where you send the data shouldn't have effect on debugging.
1
  • 1
    is there any source you can cite in terms of the semantic that for post requests the request body should contain the data ?
    – Pfinnn
    Aug 2, 2022 at 15:42
41

The following are my rules of thumb...

When to use the body:

  • When the arguments don't have a flat key:value structure
  • If the values are not human readable, such as serialized binary data
  • When you have a very large number of arguments

When to use the query string:

  • When the arguments are such that you want to see them while debugging
  • When you want to be able to call them manually while developing the code e.g. with curl
  • When arguments are common across many web services
  • When you're already sending a different content-type such as application/octet-stream

Notice you can mix and match - put the the common ones, the ones that should be debugable in the query string, and throw all the rest in the json.

7
  • 73
    Selecting how to structure your API based on development convenience is not a good practice.
    – Eric Stein
    Aug 19, 2014 at 14:42
  • 2
    Like @EricStein said, you've got it backwards.
    – DanMan
    Aug 19, 2014 at 20:11
  • 43
    Guys, the reason I asked the question is to get the right answer. Go ahead, write an answer and I'll remove my flawed one. @EricStein Aug 20, 2014 at 11:41
  • 4
    @Jonathan apis that are easy to consume via human hands are almost always good apis. Kudos for accurately calling out KISS Feb 20, 2018 at 16:56
  • 1
    @AkshayHiremath He's referring to the fact that you might be sending something else in the body, e.g. if you sent a ContentType header like "image/jpeg" you would need to have your message body contain the jpeg data and could not include anything else in it
    – shayaan
    Feb 26, 2020 at 16:20
17

The reasoning I've always used is that because POST, PUT, and PATCH presumably have payloads containing information that customers might consider proprietary, the best practice is to put all payloads for those methods in the request body, and not in the URL parms, because it's very likely that somewhere, somehow, URL text is being logged by your web server and you don't want customer data getting splattered as plain text into your log filesystem.

That potential exposure via the URL isn't an issue for GET or DELETE or any of the other REST operations.

0

According to Service Design Paterns (2011) by R. Daigneau, a service can leverage the Tolerant Reader pattern. This ensure that the service function properly when some of the content in the messages or media type they receive is unknown or when data structure vary.

The software is likely to be developed by small incremental pieces over time, and yet the system design evolve naturally. Unfortunately, it introduce the possibility of breaking change as data items are added to, changed, or removed from the message, by any stakeholders. The service must be forward compatible and under certain condition accept content that it does not fully understand. Exceptions are only throw when the message content clearly violate business rules.

In complement to other answers, we can imagine a service that accept message data from both the request body and the query string for the same requested item representation. This to be compatible with evolving clients.

0

Current best practices for Get uses Routes and Query Params, not bodies. According to some posts I've read, there are some systems that will throw failures when trying to read a GET with a body.

However, I believe this system and way of thinking is outdated and it is well proved by simply reading this post and others. It is my personal opinion that GET requests should read something like the below to allow for HIPPA laws and privacy practices to be best implemented. Query strings and urls are easily read while sitting between two endpoints, leading to some crazy stupid query strings and other behaviors (like sending Gets in Posts) that simply are a symptom of this outdated practice. Unfortunately, I am not a major cog in the machine to elicit such change.

We should, very much consider a change to do this as practitioners and pioneers of our art, but we should also maintain the newest and current best practices until that time:

get/Humans?page=2&sort=desc
{
Name: "Bob"
Age:"44"
Region:"Greece"
}

aka

get/route?metasearchinfo
{
   "private":"searchInfo"
}

The proposed example has HIPPA type data and private data in the body of a get, while having meta search data in the query string, and the exact URL path where we will query this data before that query. In this query, we betray no sensitive information, we aren't pretending a Post, and we are keeping things organized.

Again, you can feel free to disagree, but I think this kind of GET makes sense for a future that upholds privacy as an important ideal. Unfortunately, it isn't best practice at this time and will cause issues with some systems.

2
  • The answers above, if followed to an absurdity lead to: Get/Bob/SSN or Get/Bob?SSN=#####... or Post/Bob { SSN: ##### } (to act like a Get) And we see this answer is wrong because it exposes user data violating laws or doesn't meet the community standard or we encode a query string. If the answer is wrong or absurd, we should modify the standard to what is right... Oct 24 at 13:30
  • baeldung.com/… is a post about get with bodies at this time, which have been rejected up until 2019 at least. Oct 24 at 14:03

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.