Saving a Postman header value into a variable throughout requests in a collection

jmcode picture jmcode · Feb 21, 2017 · Viewed 27.2k times · Source

Good afternoon Stackers,

I am trying to automate my test suite in Postman so that I don't have to manually go into each request and change that header value to what I initially put in the first request.

My test suite currently looks like:

First Request:

var headerValue = postman.setGlobalVariable('Number', headerValue);
console.log("Number is: " + headerValue);

Second Request Header:

Number - {{headerValue}}

I would expect headerValue to have the value of 'Number' since I have set it as a global variable but it is coming back as undefined. I'm not sure what I am doing wrong.

Answer

Sai Ram Reddy picture Sai Ram Reddy · Aug 25, 2017

This is how you can do this

If Refresh_token is the header value

postman.setGlobalVariable("refresh_token",postman.getResponseHeader("Refresh_token") );

Official Documentation: https://www.getpostman.com/docs/postman/scripts/test_examples