Making REST Query Params conditional?

So I have a rest api that gets the index list on my main page:
{{url}}/api/jobs
I want users to filter for a category of jobs. My backend API (Strapi) has a filter feature:
{{url}}/api/jobs?filters[professional_field][$eq]=${properties->get('category')}
However, if the property value is empty or null, it filters everything and my filter doesn’t work how I expect it to. Strapi wants the entire param removed if I don’t filter by it. Is there a way to remove the query entirely if the value is falsy?

I would suggest duplicating the request, and then in builder you can have 2 sources set on 2 separate div elements, and using visibility conditions you can choose which one to call.

Apart from that, I don’t see any other solution…

Strapi is open source, I’ll see if I can figure it out on the backend side.

Also maybe could work:

Instead of

?filters[professional_field][$eq]=${properties->get('category')}

To

?filters=${properties->get('category')}

Then in builder, send whole string as [professional_field][$eq]=something

1 Like

I’ll let you know if I can figure it out.

If anyone comes upon this post in the future, I solved the problem using Strapi route middleware. I started with this blog post: Route-Based Middleware to Handle Default Population Query Logic

And modified ctx.query for my use case.

Alright @Dijan, I ran into a problem. I need the parameter values to be literally blank. However if I leave them blank in Divhunt, they output the string “null”

In postman this works great:

But this is what divhunt does in the REST API:

CURL Request Preview

curl --request GET ‘https://url.com/api/jobs?filters[$and][0][professional_field][$eqi]=null&filters[$and][1][location_option][$eqi]=null&filters[$and][2][$or][0][title][$containsi]=null&filters[$and][2][$or][1][descr][$containsi]=null
–header ‘Authorization: Bearer XXX’


Any ideas on the default value to get a literal empty string, instead of “null”?

@will_lutz

Does it still say null if you put it like ${properties->get(‘search’, ‘’)}?

I never responded to this, but no, the extra blank text parameter still leaves the query param value as “null”