8

I can fetch data from json-server (fake server) by calling:

http://localhost:3000/posts?title_like=head&comments_like=today

This will return records where title likes "head" AND comments like "today". What if I want not AND but OR operator?

4
  • but how? it's fake json-server Commented Jun 10, 2019 at 15:02
  • You'll probably need to use a custom route/middleware to handle this.
    – Someone
    Commented Jun 10, 2019 at 15:13
  • I can't understand how they can help me out Commented Jun 10, 2019 at 19:32
  • Have you figured out a way yet? I'm also struggling with this :(
    – DatGuyKaj
    Commented Jun 25, 2020 at 12:54

2 Answers 2

0

There are multiple ways to implement routes, just like @lewis mentioned too. If you haven't implemented routes yet, maybe it is a good place to start (if you have to provide inputs in a URL).

"...title_like=head&comments_like=today" is just a URL, and I do not believe URL is best place to specify "OR" logic. It should be up to your server side code to decide whether or not to use "AND" or "OR" logic between provided parameters.

If you did end up using above URL, and server side code did in fact implement an "OR" logic, your clients may get confused, because they maybe expecting "AND" behavior, especially if they did not read your documentation.

Another alternative is to provide inputs in a body parameter, instead of a URL. Body parameter can be and are used for both "AND" and "OR" logic. That way you can also communicate to your users that both values from the body will be used for "OR" logic, not "AND" logic (again, if your clients do read the documentation)

1
0

If it's on the same field, you can use regex for this

GET /posts?title_like=(?:hello|world)

If it's on similar fields as far as I know you can't without making 2 requests and then joining them. A while ago I made a fork for a different purpose but it can help with what you want:

GET /posts?q=search-text&attr=fieldName1,fieldName2,fieldName3,etc

This will search search-text within the specified fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.