Hello,
I’m setting environment variable from Body response like this:
var response = JSON.parse(responseBody);
pm.environment.set("variable", response.body_parameter);
But I would like to do same thing with value from Headers e.g to set “content-length” value as environment variable:
Could you please advise?
bpricilla
(Pricilla B)
July 1, 2021, 3:13pm
2
@docking-module-tech5 Welcome to the community Please refer the similar post discussed here:
Hi,
Is there a way to access the Response Headers with a Postman test? I am making a call to check the Status of a website, but would also like to console log the Server and Content-Type values that are returned. thanks for any insights
1 Like
Hi @docking-module-tech5
You can try as below
var contentLength = JSON.stringify(pm.response.headers);
console.log(contentLength);
pm.environment.set('contentLength', JSON.parse(contentLength)[2].value);
console.log(pm.environment.get("contentLength"));
1 Like
neilstudd
(Neil Studd)
July 1, 2021, 3:30pm
4
If you’re interested in getting this value as a one-liner, you can retrieve the headers with pm.response.headers
and then use the HeaderList syntax to grab the specific header by name:
pm.environment.set("contentLength", pm.response.headers.get("Content-Length"));
1 Like