Run JavaScript in a body of a REST query

I have a API REST POST query and I am trying to generate a body with JavaScript. I set the Body type to Raw and I typed simple JavaScript like this:

{{
var x = 5;
return x * 2;
}}

It does not work. The goal is to write JavaScript that produces JSON but whatever JavaScript I write I always get an error: An unexpected token has been found. Please make sure that your JS syntax is correct.


No curly braces needed in JS query

Its not a JS query. Its in the body of a REST API HTTP request

@Zvi , this does not seem to work. Why not create a js query or transformer that dynamically create the body and reference the return value in you rest api query?

Because I need to reference the table.changeset array in the body and pass that as a param to the JS function. I suppose it could be done, but why does JS not work directly in the body, is it a bug?
I will try your idea, thanks!

Your idea does not work, although I successfully create a JS query (called it query17) I cannot reference it in the body of my REST API request as again I am told that there is an error in my JS syntax
image

@Zvi , remove the {{}} on line 1 and 8, as those are not required in a JS query

Removed, its now just a plain string - see the body

Is that in the body of your rest api query? And query17 is your JS query?

Yes and Yes

Just put the js that creates your body in js query17 and return it.
Then use query17.value in your rest api body.
Trigger your rest api query from query17 using js or on succes event handler.
You’re now trying to trigger query17 from the rest query and passing values to it.

In the REST API body you should use inline javascript i.e. one javascript expression instead of a javascript block. Pls notice that return is should be used within the body of a function.

You should not use ; within the {{ }}, because ;should be used in javascript block; Also the trigger will return a Promise instead of resolved result, so you can't get the real value of result here.

Of course, it should be resolved to string, you should wrap the js expression with {{}} in rest api body.

I think the more directly way is to define a function and use it in rest body.

Thank you @AnsonHwang that works for me! Now the challenge is getting several lines of JS into one expression. Definitely possible!