Lev
Lev

Reputation: 15734

should I pass query parameters in the url or in the payload?

Suppose I have a REST endpoint like this :

http://server/users/query

And I have parameters in my query : age, city, country

I want to do a GET request with those parameters.

Should I better pass the parameters in the url ? Or put something like this in the payload of my GET request.

"query": {
    "age": "something",
    "city": "something",
    "country": "something"
}

Upvotes: 0

Views: 3317

Answers (2)

cassiomolin
cassiomolin

Reputation: 131037

On my understanding, you have a collection of users and you want to get a representation of it. You should consider query parameters to filter your collection, as following:

http://[host]/api/users?age=something&city=something&country=something

And avoid GET requests with a payload. See the quote from the RFC 7231:

A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.

Upvotes: 2

pedromss
pedromss

Reputation: 2453

From MDN: GET requests (typically) don't have bodies so use the query parameters or the path.

If you are making requests to a server you should instead read the documentation of it's API.

Upvotes: 0

Related Questions