TopHome
<2024-08-19 Mon>tech

Request body in GET

If you want to setup a GET endpoint, with a free set of query params, which is any (or a large) number of keys with values, you might think that sending a json is the way to go.

But, apparently not. The problems:

  1. You can send JSON in the request body, but this is "non-standard" behaviour. Which means corner cases can get triggered in caches and proxies (which presumably only use the params in the url to decide on action).
  2. You can URL encode the json into a single query param value. But, this means that the URL can get really long. How long is not exactly clear, but this is icky.
  3. You can split up the json into a known set of key-values and encode each as a separate query param. But, we don't want to do this.

So, what can we do? Use POST instead of GET. This sounds crazy, but apparently, this is the best option, even in 2024.

Some references:

  1. https://news.ycombinator.com/item?id=20609504
  2. https://stackoverflow.com/questions/978061/http-get-with-request-body?noredirect=1&lq=1