# I can't get the sixth element from the header array

**URL:** <https://community.postman.com/t/i-cant-get-the-sixth-element-from-the-header-array/59937>\
**Category:** Help Hub\
**Tags:** response, array, pmsendrequest, pre-request-script, sendrequest\
**Created:** [April 14, 2024, 10:39pm UTC](https://community.postman.com/t/i-cant-get-the-sixth-element-from-the-header-array/59937 "2024-04-14T22:39:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![garibzyanovsergei](https://avatars.discourse-cdn.com/v4/letter/g/50afbb/32.png) [@garibzyanovsergei](https://community.postman.com/u/garibzyanovsergei)\
**Post date:** [April 14, 2024, 10:39pm UTC](https://community.postman.com/t/i-cant-get-the-sixth-element-from-the-header-array/59937/1 "2024-04-14T22:39:48Z")

</div>

> Why I can’t get the sixth element from the header array from the response, because it’s there?

I have the code in the pre-request scripts:

```auto
pm.sendRequest({
    url: pm.environment.get('baseUrl') + '/checkPhone',
    method: 'POST',
    header: {"Authorization": `Bearer ${pm.environment.get('accessToken')}`},
    body: {
        mode: 'raw',
        raw:JSON.stringify({
            "phone": `${randomInt}`,
            "sendAuthCode": false
        })
    }
}, (err, res) => {
    pm.test("Status code is 200", function () {
        res.code === 200;
    });
    
    console.log(res);
    console.log(res.status);
    console.log(res.code);
    console.log(res.header[6]);
});

```

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [April 15, 2024, 10:20am UTC](https://community.postman.com/t/i-cant-get-the-sixth-element-from-the-header-array/59937/2 "2024-04-15T10:20:43Z")

</div>

Not exactly sure what is going on here, but the key is called “headers” despite what the console log is showing you.

Try the following…

```auto
console.log(res);
console.log(Object.keys(res));
console.log(res.headers);

```

The following is a basic sendRequest to Postman Echo.

The screenshot clearly shows the key as “header” when you console log the response, but when you console log the Object keys, its shows “headers” which does actually work.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/d/b/db8328b077a87848abb3b5bd758a1b33f36f08e9.png)

\*\* EDIT \*\*

I think I do know what is going on here now, you haven’t parsed the response.

```auto
console.log(res.json());

```

If you do this, you will correctly see the key as “headers”.

---

<div class="post-metadata">

**Author:** ![garibzyanovsergei](https://avatars.discourse-cdn.com/v4/letter/g/50afbb/32.png) [@garibzyanovsergei](https://community.postman.com/u/garibzyanovsergei)\
**Post date:** [April 15, 2024, 12:24pm UTC](https://community.postman.com/t/i-cant-get-the-sixth-element-from-the-header-array/59937/3 "2024-04-15T12:24:31Z")

</div>

Thank you for responding, but even with the headers key, it is not possible to get any of the array elements. As for the `res.json()` option, in this case I only get the response body without headers.

```auto
...
    console.log(res);
    console.log(res.headers);
    console.log(res.headers[6]);
    console.log(res.json());

```

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/7/4/74fc23775cebc0167f3904dd9b01cdd3b2ea5d6c.png)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [April 15, 2024, 1:00pm UTC](https://community.postman.com/t/i-cant-get-the-sixth-element-from-the-header-array/59937/4 "2024-04-15T13:00:02Z")

</div>

I’ve had a look and you are correct. You can’t target any of the headers that way.

```auto
console.log(res);
console.log(typeof(res.headers));
console.log(res.headers);

pm.test('headers should be an array', () => {
    pm.expect(res.headers).to.be.an('array').that.is.not.empty;
});

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/9/a97c2b283956f00d1950798c24c74fdc64cd7c94.png)

I think I’ve seen this before. It’s technically not an array, its a list which is why you need to parse the response.

I guess whether the headers exist once you parse a response, will be down to the API specification.

On a normal request, you can use the pm.response method, but I’m not sure if there is an equivalent for sendRequest().

You could also try.

```auto
console.log(res.toJSON().header[6]);

```

---

<div class="post-metadata">

**Author:** ![garibzyanovsergei](https://avatars.discourse-cdn.com/v4/letter/g/50afbb/32.png) [@garibzyanovsergei](https://community.postman.com/u/garibzyanovsergei)\
**Post date:** [April 15, 2024, 5:16pm UTC](https://community.postman.com/t/i-cant-get-the-sixth-element-from-the-header-array/59937/5 "2024-04-15T17:16:55Z")

</div>

Thanks a lot for the reply. I managed to extract the value I needed as follows:

```auto
console.log(res.toJSON().header[6].value);

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [April 18, 2024, 5:17pm UTC](https://community.postman.com/t/i-cant-get-the-sixth-element-from-the-header-array/59937/6 "2024-04-18T17:17:05Z")

</div>

Closed on April 18, 2024, 5:17pm UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
