POST request with variables and raw body JSON

I’m new to postman and API. I read and followed the posts and the results I am seeing does not match what others have posted and I’m open to any help.

I have a simple POST request that I setup as a test that creates a group via API. The JSON body in the request that is tested and functional without variables is:

    "name": "Supreme"

With my variable, the body looks like this in the request:

    "name": "{{name}}"

My pre-request script that creates the variable:


When I send with the variable in the body, the Postman Console shows the request body with the properly substituted value for the variable which means the variable was set and should be correctly communicated out but the response provides a null data body and 1401 error.

    "data": null,
    "errmsg": "Authentication failed",
    "status": 1401

If I edit the request JSON body and only change the variable from “{{name}}” to my value i.e. “Supreme” and send then it works correctly.

"status": 200,
"errmsg": "OK",

So what am I missing?

1 Like

Hi @iTGears ,

So, I ran through this scenario and if using just this code, it works without issues for me. So, what this potentially means is that you have an Environment Variable that is overriding it. So, before doing any other troubleshooting, I would recommend checking if you have an Environment Variable setup called “name” with a value of “”. That would be the first thought.

The other thing that you can check is to change the collectionVariable to just a variable (which is local to the request) and should override anything else.

Hope this helps!

Thanks for the response. No environmental variables or global were set. I checked out using a var and the var’s scope is local to the pre-req script so it no longer exists when the call happens and creates other errors.

Oh, sorry about that. I should have been more specific with the variable.

Instead of using:
pm.collectionVariables.set(“name”, “Supreme”);

try this:
pm.variables.set(“name”, “Supreme”);

if you use just “var” you are correct, that will only exist for the Pre request script, but if you use the pm.variable it is local to the Request, not just the Pre or Tests.

As for the fact that you dont have Environment or Global variables with the same name, I think the assumption is that you are attempting to use the same collectionVariable for your Authentication as the Request. I am thinking this since this Request is supposed to just create a group but it fails on authentication instead of a data validation for the request. So, the other suggestion would be to change the name of the collectionVariable that you are using for the group name, like

pm.collectionVariables.set(“group_name”, “Supreme”);

Hope this helps!