Reputation: 55
How can I fetch the value of a response header in postman and save it in a variable so that I can use it in the next request?
Example:
HeaderName: HeaderValue
AESKey: ndowijdw92n9992n
I need to fetch ndowijdw92n9992n
and send it to the next request.
Upvotes: 4
Views: 16035
Reputation: 797
@Srđan Popić has provided the correct answer and explanation, however with that I would like to add condition to check the response first, just to avoid unnecessary execution of the actual code:
if(pm.response.code === 200) {
pm.environment.set('token', pm.response.headers.get("AESKey"))
}
Upvotes: 1
Reputation: 1271
There is more direct way to access AESKey
header:
const responseHeaderAESKey = pm.response.headers.get("AESKey");
pm.environment.set("AESKey", responseHeaderAESKey );
Now the environment variable set contains the AESKey and you can access it in any part of Postman request with {{AESKey}}
. So use it in the next request.
Upvotes: 9
Reputation: 448
You can access the response headers using pm.response.headers
. Try placing the below code in test tab of the request which writes the response header value of 'HeaderValue' to the environments and you can access the variable value in next requests. Refer this.
var reponseHeaders = pm.response.headers.all();
reponseHeaders.forEach(function(header){
if(header.key == "HeaderValue"){
pm.environment.set("Key", header.value)
return;
}
})
Upvotes: 1