Use Environment.Set Variable in URL and Body

I’m attempting to create a request that uses a custom variable in both the request URL and Body. If I create a Pre-request Script variable like this:

pm.environment.set("Id", "test");

then everything works just fine. However, if I do something like this:

pm.environment.set("Id", "{{$guid}}");

then I end up with a different value in the URL and the Body.

I assume this has something to do with the the {{$guid}} variable value being created by a function. With this in mind, I updated my Pre-request Script to assign the {{$guid}} variable result to a const local variable… but I still see the same behavior.

const value = "{{$guid}}";
pm.environment.set("Id", value);

Is it possible to use the {{$guid}} variable in this case?

Thanks!

Hey @Everett_Comstock

This would need to be like this in the script:

const value = pm.variables.replaceIn("{{$guid}}")

You cannot directly use the {{...}} syntax in the sandbox so that’s where .replaceIn() comes in to help out.

@danny-dainton,

Thanks very much! That made quick work of my issue!

1 Like

that may help you :https://learning.postman.com/docs/sending-requests/managing-environments/ @Everett_Comstock

1 Like