Using variables in body with RAW/Text

Hi *,
I’ve searched but could not find anything that solves my problem.

I’m trying to use variables in the body of a POST request. I’ve tried to use “{{variable}}”, but it does not work.

I think the problem is the ‘content-type’ we are using for this internal end point is a custom.
application/xxxxxxxxxxx+json

If is use data instead of variables, the request works correctly. However when I add the variables, the request fails.

How do I use variables if the body is set to Raw/text?

Hi @abalagta, welcome to the community! :wave:

I guess there’s some other issue with the script / the variables not being set properly, because irrespective of the type of request body, the variables will always get resolved.


As you can see above, I have a request body with “text” selected as type.


And as you can see above, the variable t has been correctly resolved.

To debug further, you can check the outgoing request using the Postman Console.

Also, I’d recommend that you update to the latest version of the native app - v7.5.0 (as of writing this post).

I have a odd scenario where a 3rd party API I need to POST to, requires a request body text/plain. I did a pre-request script of:
let authKey = pm.collectionVariables.get(“authn”);
console.log('var: ’ + authKey); # Shows a valid value
pm.collectionVariables.set(‘reqBody’, authKey);
And my body is raw/text with a placeholder of:
{{reqBody}}
But when I POST, the body ends up being blank e.g. my token is not replaced. What am I doing wrong?

@stuart.williams1

Console log the reqBody environment variable after you set it.

pm.collectionVariables.set("reqBody", authKey);
console.log(pm.collectionVariables.get("reqBody"));

What is being sent in the body of the request? Does it show anything in the console logs if you expand out the request body section?

It also looks like you are retrieving an collection variable called authn and then not doing anything with it before resaving it as another collection variable. Why can’t you just use the original collection variable?

Finally, you might want to check the Content-Type header to ensure that is not causing you issues.

Turns, out if you select JSON even if it is not JSON, the variable is replaced. Set to Text the variable is not replaced.

image002.jpg

1 Like