HTTP Request - Pass Dynamic Variables to Request Content

,

:bullseye: What is your goal?

Send an API request to undetectable.ai

:thinking: What is the problem & what have you tried?

I just want to pass a dynamic variable to an API request for undetectable.ai and I have put the request content as attached.:

The variable 2.text_in is a set variable which is not null.
However, there is an error as the attachment.

Please help.
One note: When I use the plain text, instead of the above set variable, the request goes through successfully.
Thanks.
Kind regards,
Sam

:clipboard: Error messages or input/output bundles

Attached

:camera_with_flash: Screenshots (scenario flow, module settings, errors)

The variable 2.pdf (127 KB)

Hi Sam,

Unless your variable is an integer (ie a number) you need to put the variable inside quotes as with the hard coded values.

ie

”content”: ”{{2.text_in}}”

At the moment as it is not inside quotes it is not a valid JSON structure.

Let me know if that helps.

James

1 Like

Welcome to the Make community!

Escaping strings before mapping into another JSON string

Most of the time, you cannot directly map variables containing text content into another JSON string without escaping them, as the variable might contain special characters that are reserved. Special characters in JSON needs to be escaped (encoded) as a “literal” character, otherwise they make the whole JSON invalid when you map the variable value when creating another JSON string.

You can escape string variables by passing the text into the “Transform to JSON” module. You can also build a valid JSON string by using a Data Structure with the “Create JSON” module. Then, you can map the output of the Transform module into another JSON string, or simply use the whole output of the Create module.

:warning: Note:

When using the “Transform to JSON” module, the output should already contain the double quotes " " around your text, so when you map this output into your JSON, you should not need to wrap another pair of double quotes around it.

Alternatively, you can use the built-in function replace to replace those special characters with their escaped versions. To do this, simply paste this into the field, and replace the 1.text variable:

{{ replace(replace(1.text; "/([""\\])/g"; "\$1"); "/\r?\n\r?/g"; "\n") }}

For more information on JSON and escaping JSON strings, see:

Hope this helps! If you are still having trouble, please provide more details.

— @samliew
P.S.: investing some effort into the tutorials in the Make Academy will save you lots of time and frustration using Make!