Running collection in newman. Collection variable is not passing into request URL

Hello Everyone

I’m running collection in Newman. I’m getting id in response and save it as collection variable and I’m using this variable in the following request URL. While I’m running these requests in Postman I don’t face any issues but when I’m running them in Newman collection variable is not passing to request URL. Where can be the problem? Because I have similar requests in the same collection and they run well in Newman.

Here is the first request where I upload file and receive document id. I store received document id as collection variable “fileId”

Here is the following request where I pass stored document id into request url and receive downloaded file

As you can see in Postman it runs well.

Here is my run configuration in Newman
newman run “C:\Users\42073\Postman\files\Test Stage.postman_collection.json” --folder “Token Request” --folder “Storage” -e “C:\Users\42073\Postman\files\Test Stage Environment.postman_environment.json” -r htmlextra

In report I can see that collection variable is not passing into request URL

I don’t understand where is the problem. Because the similar request are running ok, where I store id and pass it as variable into request url. But exactly with this request it doesn’t work.

I will be grateful for any advice

Not sure if this is causing the issue, but in your first request, declare your variables properly. Using const, let or var.

If you don’t declare the variable, it creates a global variable, which may not be available in the collection runner or Newman. (I haven’t tested it, but then again I always declare the variables).

I always recommend to declare the variables unless you really do want it to be global like defining a global function in a pre-request script).

Also why are you parsing the response using the old method, creating the fileId variable with the same data, and then parsing the response again in the second test. Do it once at the top of the script using…

const response = pm.response.json()

Having the collectionVarible and the local variable with the same names can also cause scope issues, so call them something else. So you can differentiate them.

If you response really is just a single string, you can set the collection variable directly from here.

pm.collectionVariables.set("fileID", response);

The test then becomes

pm.expect(response).to.be.a("string");

You could also create a test to ensure that the collection variable is set correctly. (As that is what you really want to know). It will also help troubleshooting.

pm.expect(pm.collectionVariables.get("fileID")).to.eql(response);