I have an API endpoint to retrieve all users. There are 3 query parameters for searching/filtering the results as follows.
GET /users?name=test&age=23&area=abc
Now I want to introduce an option to ignore the case when searching for the name
parameter. For example, the above API call should return even if the name equals Test
or tesT
.
What's the correct way of implementing this option? Adding another query parameter or is there any better way of implementing it?
CodePudding user response:
In this specific case, an easier option could be to define the query parameter value as a regex expression, since regex expression itself allows us to define a string to be case insensitive / sensitive.
In other scenarios, another option would be to incorporate the specification (that the value needs to be case insensitive) into the query param value itself, like
http://localhost:3000?name=case_insensitive(test)
CodePudding user response:
I would realize two new parameters for name, namely: nameIgnoreCase and nameCaseSensitive. In this case the user of the endpoint can and must decide. If this is well documented, the user gets an additional hint that this ‚question’ exists at all. You can also continue to provide name as the default behavior, which will fall back to either nameIgnoreCase or nameCaseSensitive.