hi ,
I am hitting the token URL to get the Bearer token, Now, I want to save this token in the environment variable so that I can use it for subsequent request. I tried to add code in the Tests tab but I get the following error
const responseJson = pm.response.json();
There was an error in evaluating the Pre-request Script:SyntaxError: Unexpected identifier
I have not added anything for the Pre-request Script. So why do I get this error?
Is that the full script that you have in the Tests tab?
Do you have any code in the Pre-request at the Folder or Collection Level? You can open these entities by click on them in the sidebar, to view the information at that level.
Yes, I was just trying if this works so I do not have the full script added yet.
Turns out I had some script added in the collection which was causing the issue
Thanks for your help!
hi @danny-dainton , sorry I had a follow up question. My script works fine when I run the individual request and it sets the AuthToken env. variable value as well.
const responseJson = pm.response.json();
var bodyToken = responseJson.access_token;
pm.environment.set("AuthToken", bodyToken);
console.log(" value is"+pm.environment.replaceIn('{{AuthToken}}'));
But If i run the collection that has this test script, the script is not able to set the AuthToken env. variable. I printed the AuthToken value and it is blank too. Is there a reason for the Test script not running properly when I run it from the collection?
Have you save all the changes in your requests before running the Collection? If you have unsaved changes in the request, the Runner wouldn’t pick those up.
It might be worth enabling the Autosave option from the Settings to ensure that these are all saved.