0

I'm developing a node application which has an endpoint where it filters stories.

I accept multiples query params up to five. I don't know if this is too much query params but I need these params to filter in the data base.

Example:

http://localhost:3000/api/search?location=wherever&duration=123

I was wondering if there is any way better to do it like:

http://localhost:3000/api/search?filter=location:wherever,duration:123

or even I could send a stringify object from the url but it seems an ugly solution for me.

The second way I put here looks cleaner for me but I'm not sure how to handle the parameters.

In the first option I can pass req.query to my service and handle the object however in the other way handle the filter value would be harder.

What do you think? How do you manage this case to send a filter to your back?

Thanks in advance and my apologies if it was already answered, I didn't find a similar post.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

1

What is the maximum query params url I should use?

There is no limit to the number of query parameters and it doesn't cause any problem to have lots of them.

I accept multiples query params up to five. I don't know if this is too much query params but I need these params to filter in the data base.

Five is low, not high - you're absolutely fine.

The practical limit to the total URL length is around 2000 characters so you just want to make sure your URLs stay comfortably below that and not worry about the count of separate query parameters.

I would only consider an alternative way to send the query if the total URL length was getting too long or could be too long. Aside from that, you can have as many query parameters as you want. It's not really something a human consumes directly, it's just something your code generates and parses so the quantity really doesn't matter.

That said, if you had 100 separate query parameters that would all be used at once, I'd wonder what you were doing and if there was a better way to accomplish it or express the query, but it wouldn't be a technical problem or limitation.

The structural alternatives are using POST instead of a GET and sending data as JSON or something like that. You can send as much data as you want that way, but a POST changes the usability of the URL since it's not something that can just be directly linked to or bookmarked or copied into a URL bar like a GET can.

jfriend00
  • 683,504
  • 96
  • 985
  • 979