# Setting value from Headers as environment variable

**URL:** <https://community.postman.com/t/setting-value-from-headers-as-environment-variable/25405>\
**Category:** Help Hub\
**Tags:** tests, variables, authentication\
**Created:** [July 1, 2021, 2:54pm UTC](https://community.postman.com/t/setting-value-from-headers-as-environment-variable/25405 "2021-07-01T14:54:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![docking-module-tech5](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@docking-module-tech5](https://community.postman.com/u/docking-module-tech5)\
**Post date:** [July 1, 2021, 2:54pm UTC](https://community.postman.com/t/setting-value-from-headers-as-environment-variable/25405/1 "2021-07-01T14:54:27Z")

</div>

Hello,

I’m setting environment variable from Body response like this:

```auto
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:

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/c/ca0ffaf4deb8ac3835d73101bcffaef1a9ca0df7.png)

Could you please advise?

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [July 1, 2021, 3:13pm UTC](https://community.postman.com/t/setting-value-from-headers-as-environment-variable/25405/2 "2021-07-01T15:13:49Z")

</div>

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

> [@Access Response Headers with Postman Tests](https://community.postman.com/t/access-response-headers-with-postman-tests/4654):
>
> 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

---

<div class="post-metadata">

**Author:** ![jency.stella19](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jency.stella19/32/16633_2.png) [@jency.stella19](https://community.postman.com/u/jency.stella19)\
**Post date:** [July 1, 2021, 3:18pm UTC](https://community.postman.com/t/setting-value-from-headers-as-environment-variable/25405/3 "2021-07-01T15:18:08Z")

</div>

Hi @docking-module-tech5

You can try as below 🙂

```auto
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"));

```

---

<div class="post-metadata">

**Author:** ![neilstudd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/neilstudd/32/8213_2.png) [@neilstudd](https://community.postman.com/u/neilstudd)\
**Post date:** [July 1, 2021, 3:30pm UTC](https://community.postman.com/t/setting-value-from-headers-as-environment-variable/25405/4 "2021-07-01T15:30:06Z")

</div>

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](https://www.postmanlabs.com/postman-collection/HeaderList.html) to grab the specific header by name:

```auto
pm.environment.set("contentLength", pm.response.headers.get("Content-Length"));

```

---

<div class="post-metadata">

**Author:** ![docking-module-tech5](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@docking-module-tech5](https://community.postman.com/u/docking-module-tech5)\
**Post date:** [July 2, 2021, 6:30am UTC](https://community.postman.com/t/setting-value-from-headers-as-environment-variable/25405/5 "2021-07-02T06:30:23Z")

</div>

Thank you for help! 😀
