Ignore URL params when null

I want to send a GET request with URL params specified by some components in retool. The issue that I have is that when the components are empty I don't want the params to appear at all (instead of sending null.
I tried different things:

  1. Adding the URL params normally, but this results in the API telling me that None is not a valid choice.
  2. Using an empty string when the field is not selected results in the API telling me that " is not a valid value:
  3. Manually building the URL, saving it in a temporary state and adding that temporary state to the URL results in the ? being encoded as %3F:
    Screenshot 2022-09-19 at 09.47.47

What are my options here? As soon as I try to move the ? to the URL retool identifies it as a URL param and adds another = to the end of the query which makes it fail.

Hey @ktrebing!

You can disable url parameters by passing your conditional to the key field instead of the value field. Can you try something like this?

key:

{{ projectFilterSelectBudget.value ? "budget" : null }}

value:

{{ projectFilterSelectBudget.value }}
5 Likes

Amazing! Thank you!

Ooow, I wouldn't think about that
Clever approach!

1 Like

Hey @Kabirdas , I noticed when the value entry is more complex, this extra query parameter will still be added to the url. eg:

Do you have a solution to my case?

More specifically, I noticed the value being
1/ {{multiselect1.value?international_categories:[${multiselect1.value}]:null}}
2/ {{international_categories:[${multiselect1.value}]}}
and
3/ {{multiselect1.value}}
gives me different results for key being null case. where 1/ and 2/ will be nonnull value and the url param still added, and 3/ case not added

:thinking: the value of an empty multiselect is an empty array which is still truthy, I wonder if that's what you're running into here. Can you try {{ multiselect1.value.length === 0 ? 'filter_by' : null }}?

1 Like

@Kabirdas

I tried with {{ multiselect1.value.length === 0 ? 'filter_by' : null }} , the behavior remains the same as my previous description.

Also, I think empty array evaluates to Falsy.

Do you have any other thoughts on possible solution? It feels like a bug on retool RestAPI query

I put the ternary in the wrong order :face_with_hand_over_mouth: it should be {{ multiselect1.value.length === 0 ? null : 'filter_by' }}, sorry, can you let me know if that works? :crossed_fingers:

Aug-02-2023 18-19-31

This is a good reference for Truthy/Falsy values! A good alternative to relying on JS type coercion or checking length is to use the built in lodash _.isEmpty function which is more readable.

It worked! Thank you so much @Kabirdas !!