Setting a REST API body via additionalScope

Hello Retool Community, this is my first post, and I would appreciate your help.

When a user submits a form, I want to make a PUT request to a REST API. The parameters to send in the request body vary depending on the fields entered on the form, for example some fields should only be included in the request body when the user selects a particular option in the form. It's kind of like a dynamic form. To handle this, I created a javascript script that constructs the request body depending on the fields entered on the form then triggers a REST API resource query like so:

payload = {} // dynamically build my request payload
my_rest_api_query.trigger({ 
    additionalScope: { payload }, //attach the payload via additionalScope
    onSuccess: handleSuccess,
    onFailure: handleFailure
 })

In the REST API query, I use the "raw" body type and set the value to:

{
    {{payload}} 
}

Here's a screenshot of the REST API resource query

However, the query does not work correctly. The payload passed via additionalScope does not get sent in the request. I have been stuck on this for a while, I can't figure out what I'm doing wrong. I even tried JSON.stringifying "payload", but it still doesn't work. Please help me out. Thank you

Have you tried removing the braces in the Body of the API query which are wrapping the {{payload}} ?

image

2 Likes

@isaacjm Oh my goodness! I just tried it and it works. Thank you so much

1 Like