How can I skip a parameter from a request depending on the value?

I have a JSON API request with 4 parameters. I want to avoid sending null in one of them (if it's null, only 3 are sent). How can I do it?

Before sending you could parse the JSON to a JavaScript object, validate the parameter values and if any of them are null, remove that parameter. Stringify the object and send it on its way. Without any examples my response is a bit vague.

@juanignaciosl

Are these query parameters or body parameters? With body parameters, what @brettski said will work! For query parameters, that is not currently possible in Retool but we do have an internal request for this functionality in place.

1 Like

I am trying to do post on api from a form . Transformers are not working here. Even after hardcoding ,the values from fields are used.

var parsedData={};
parsedData=JSON.parse(data);
const body={};
Object.entries(parsedData).forEach(([key,value]) => if(value!=null) {(body[key] = value)});
return JSON.stringify(body);

I want to do this. Plz correct if i am wrong.

@Lakshay

Hey there :wave: Would you mind sharing a screenshot of the query to your API? Are you seeing any errors?

1 Like

No ,no errors ,the query runs just fine but donot consider transformer at all . It send all the fields. Its a normal post request to an api

hey @Lakshay, are you trying to do something like this another post here??

The transformer changes the data you receive in retool, not the one you are sending so the way you are thinking won't work. The answer I linked above removes the parameters that would otherwise have null/empty values. Let me know if this address your issue there

Thanks for stepping in here @jocen ! This is exactly what we would recommend.

Something I noticed in your screenshot @Lakshay, and something that @jocen alluded to is that a transformer attached to a query runs after the query. Let me know if you have any further questions!

Any update on this? The "solution" by @jocen is more of a hack workaround than a proper solution :slightly_frowning_face: