I'm working on an application that allows users to filter by location. In order to do this, I'm adding my city filters into a query parameter. For example, if someone selects California
I would encode it in base64 and then add the filters to my query parameter. Here's the filter object that gets encoded:
[{city: "San Francisco", state: "California", country: "USA"}, {...}, {...}]
Here's what the request will look like after encoding:
http://localhost:3001/api/v1/companies?offset=0&limit=50&locs=W3siY2l0eSI6IkFsYW1lZGEiLCJzdGF0ZSI6IkNhbGlmb3JuaWEiLCJjb3VudHJ5IjoiVW5pdGVkIFN0YXRlcyJ9LHsiY2l0eSI6IkFuYWhlaW0iLCJzdGF0ZSI6IkNhbGlm...
However, after a certain number of filters, I run up against header limits (i.e. I can't fit more information into the request header).
What's the best-practice way of handling this? Should I send a POST
request instead, or is that frowned upon?
FYI — Users can click into a state and select/unselected specific cities. For example, someone could click into California
and uncheck San Francisco
and Los Angeles
.
The request URI is not considered a header, but you still may run into limits. Usually it's recommended to stay under 2048 bytes.
If you go above this, switch to a different HTTP method (QUERY
, POST
) and use the request body. QUERY
is more appropriate for read operations, supported by browsers but not yet by every server. POST
is a good second choice.