Setting value from Headers as environment variable

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?

@docking-module-tech5 Welcome to the community :partying_face: Please refer the similar post discussed here:

1 Like

Hi @docking-module-tech5

You can try as below :slight_smile:

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

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"));
2 Likes

Thank you for help! :grinning:

2 Likes