0

Is it something evil to use a POST route instead of GET to retrieve data?

One of my route has 14 variables. It's really complicated to use a road without a body. So I wonder if it's a use case to use a POST route in this case.

If not, is there another method?

Exemple GET :

this._http.get(`myURL?type=${type}&offset=${offset}&limit=${limit}&var1=${var1}&var2=${var2}&var3=${var3}&var5=${var5}&var6=${var6}&var7=${var7}&var8=${var8}&var9=${var9} ....

Transform to POST :

const data = {
 var1: var1,
 var2, var2
 var3: var3,
 ...
}

this._http.post(myURL, data, { headers: headers })

Thanks !

Amaury Laroze
  • 197
  • 2
  • 12
  • Does this answer your question? [REST API using POST instead of GET](https://stackoverflow.com/questions/19637459/rest-api-using-post-instead-of-get) – traynor Feb 28 '23 at 09:11

2 Answers2

0

POST has different semantics (and future maintainers will thank you for not writing code that uses the "I'm telling you stuff" verb for a "I'm asking for stuff" request.

POST has different implications for caching.

If you mean GET, use GET.


Your concern seems to be that bashing together a bunch of strings to form a query string isn't very readable. Don't do that, it does make for code that is hard to maintain and you're failing to escape special characters in the data. Browsers provide an API for that.

const data = {
 var1: "Alice & Bob",
 var2: "2+2=4",
 var3: "Hello, world"
};

const url = new URL("/my-api/", location);
const params = new URLSearchParams(data);
url.search = params;
console.log(url.toString());
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

In your case, you can get away with using a POST request to improve code readability and maintenance. This would mean that the receiving endpoint on your server needs to be support POST requests, else you could get a 404 depending on how request validation is handled for said endpoint.

Checkout this answer for more info on when to use POST vs GET:

The TL;DR is that POST requests should ideally be used for destructive operations such as creating and editing data and GET requests should be used when fetching data on the browser as you cannot initiate a browser request with a POST.

Also, POST requests could enhance security as you're not sticking sensitive details on your URL.