joao
joao

Reputation: 451

Should I use body url encoded on GET request?

I faced a question today.

I have an API made with Node JS, but in certains endpoints, I need to receive some params with spaces.

These endpoints are GET, but my question is, is that a good practice use form-url-encoded body in GET endpoints?

What must be the best way to pass parameters with spaces to a GET endpoint?

Upvotes: 2

Views: 2961

Answers (2)

VoiceOfUnreason
VoiceOfUnreason

Reputation: 57377

What must be the best way to pass parameters with spaces to a GET endpoint?

Form urlencoded query parameters is the most common answer.

/foo?bar=look+at+the+encoded+spaces

See: application/x-www-form-urlencoded


The basic framing being that the parameters are conceptually part of the identifier for the resource that is being requested.

Upvotes: 2

Evert
Evert

Reputation: 99816

You should not use request bodies with GET. Request bodies are/should be ignored and many libraries drop them.

The purpose of GET is to get the representation of a resource at the specified URI, nothing else. All the information about the resource you want to receive the state of should be in the URI. You can encode spaces in URIs though, just use a url-encoding library.

If you are talking about 'parameters', this sounds more like a RPC call to me, and POST might be more appropriate for that. POST does allow request bodies and URL encoded or JSON bodies are fine for that.

Upvotes: 2

Related Questions