# Extracting multiple values from response header

**URL:** <https://community.postman.com/t/extracting-multiple-values-from-response-header/42309>\
**Category:** Help Hub\
**Tags:** variables, json, response\
**Created:** [December 22, 2022, 10:45am UTC](https://community.postman.com/t/extracting-multiple-values-from-response-header/42309 "2022-12-22T10:45:16Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![meatinaplasticsack](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/meatinaplasticsack/32/13383_2.png) [@meatinaplasticsack](https://community.postman.com/u/meatinaplasticsack)\
**Post date:** [December 22, 2022, 10:45am UTC](https://community.postman.com/t/extracting-multiple-values-from-response-header/42309/1 "2022-12-22T10:45:16Z")

</div>

> I have a problem with extracting multiple values (and storing them into separate variables) from response header.

In my test case, after first request I get response with some important data in the response header “location”, which I need to use in second request. To automate test case, I’m trying to chop that long URL into smaller chunks/variables that I will pass in second request as a parameters.

_My question_: How to do this?

 ![Screenshot 2022-12-22 112418](https://us1.discourse-cdn.com/getpostman/original/3X/3/6/366029b8c2396b7c817fb24840fd3617b5ba928c.png)

I’ve managed to store whole “location” header into variable and reuse it in my next request but that is not good in every case I need to cover. I’ve also tried using _split_ but I can get only 1 variable value this way.

---

<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:** [December 22, 2022, 1:30pm UTC](https://community.postman.com/t/extracting-multiple-values-from-response-header/42309/2 "2022-12-22T13:30:41Z")

</div>

Your going to have to declare each of the variables, so why can’t you just split the original response four times.

Creating a function to “parse” the location would be a lot of code.  
If you can target each key\value using split, then I’d just do it this way.

---

<div class="post-metadata">

**Author:** ![meatinaplasticsack](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/meatinaplasticsack/32/13383_2.png) [@meatinaplasticsack](https://community.postman.com/u/meatinaplasticsack)\
**Post date:** [December 22, 2022, 3:37pm UTC](https://community.postman.com/t/extracting-multiple-values-from-response-header/42309/3 "2022-12-22T15:37:51Z")

</div>

Thank you @michaelderekjones for suggestion, it didn’t come to me that I can perform 4 x splits on the same variable instead of 1. 🫡

So I tried something like this:

```auto
let upload_URL = pm.response.headers.get('location');
pm.environment.set("key", upload_URL.split('=')[1]);
pm.environment.set("name", upload_URL.split('=')[2]);
pm.environment.set("expires", upload_URL.split('=')[3]);
pm.environment.set("signature", upload_URL.split('=')[4]);

```

and only last variable is correct. The rest are incorrect because on the left side of the desired value separator is “=” and on the right it is “&”. So I need extract value between “=” and “&”. I don’t how to do this with split. Should I use RegEx or there is another way?

---

<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:** [December 22, 2022, 3:44pm UTC](https://community.postman.com/t/extracting-multiple-values-from-response-header/42309/4 "2022-12-22T15:44:39Z")

</div>

Can you post an example (as text) and I’ll have a go. (Might not be today though).

It will be via Regex, split or a combination.

---

<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:** [December 22, 2022, 4:13pm UTC](https://community.postman.com/t/extracting-multiple-values-from-response-header/42309/5 "2022-12-22T16:13:47Z")

</div>

This function seems to work.

```auto
// https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript

// let upload_URL = pm.response.headers.get('location');
let url = "https://postman-echo.com/get?key=key-123&name=name-123&expires=expires-123&signature=signature-123";

function getParameterByName(name, url = window.location.href) {
    name = name.replace(/[\[\]]/g, '\\$&');
    var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, ' '));
}

console.log(getParameterByName("key", url));
console.log(getParameterByName("name", url));
console.log(getParameterByName("expires", url));
console.log(getParameterByName("name", url));

```

---

<div class="post-metadata">

**Author:** ![meatinaplasticsack](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/meatinaplasticsack/32/13383_2.png) [@meatinaplasticsack](https://community.postman.com/u/meatinaplasticsack)\
**Post date:** [December 22, 2022, 4:22pm UTC](https://community.postman.com/t/extracting-multiple-values-from-response-header/42309/6 "2022-12-22T16:22:34Z")

</div>

> [@michaelderekjones](#):
>
> Can you post an example (as text) and I’ll have a go.

Full text:

`https://qa2.testingstore.com/api/v2.0/upload/process?key=local-804ef6daf465482188c3a5951963cc36&name=QA2_2022-12-22_15-19_Testing-API_V2.png&expires=2022-12-22T14%3a29%3a46.7116531%2b00%3a00&signature=1aE9hgpcCsM`

---

<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:** [December 22, 2022, 4:35pm UTC](https://community.postman.com/t/extracting-multiple-values-from-response-header/42309/7 "2022-12-22T16:35:17Z")

</div>

The function I posed seems to be able to handle your url string.

```auto
// https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript

// let upload_URL = pm.response.headers.get('location');
let url = "https://qa2.testingstore.com/api/v2.0/upload/process?key=local-804ef6daf465482188c3a5951963cc36&name=QA2_2022-12-22_15-19_Testing-API_V2.png&expires=2022-12-22T14%3a29%3a46.7116531%2b00%3a00&signature=1aE9hgpcCsM";

function getParameterByName(name, url = window.location.href) {
    name = name.replace(/[\[\]]/g, '\\$&');
    var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, ' '));
}

console.log(getParameterByName("key", url)); // local-804ef6daf465482188c3a5951963cc36
console.log(getParameterByName("name", url)); // QA2_2022-12-22_15-19_Testing-API_V2.png
console.log(getParameterByName("expires", url)); // 2022-12-22T14%3a29%3a46.7116531%2b00%3a00
console.log(getParameterByName("signature", url)); // 1aE9hgpcCsM

```

---

<div class="post-metadata">

**Author:** ![meatinaplasticsack](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/meatinaplasticsack/32/13383_2.png) [@meatinaplasticsack](https://community.postman.com/u/meatinaplasticsack)\
**Post date:** [December 22, 2022, 4:45pm UTC](https://community.postman.com/t/extracting-multiple-values-from-response-header/42309/8 "2022-12-22T16:45:26Z")

</div>

That is awesome but to be honest I need some time to understand what is going on here. 🤯 😀 I started to explore other JS methods like substr() and substring() but this… 🥹 Thank you very much! 🫡

---

<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:** [December 22, 2022, 5:08pm UTC](https://community.postman.com/t/extracting-multiple-values-from-response-header/42309/9 "2022-12-22T17:08:49Z")

</div>

Postman uses JavaScript a lot so its worth while getting your head around the basics.

JSON, JavaScript objects, and arrays for a starter. Regex is also something worth learning.

When you get something complicated, I always look to Google first.

No point in trying to re-create something that has been done hundreds of time before. This includes Regex examples.

The post on Stackoverflow that this function comes from has been upvoted 10019 times, so I’m fairly certain it would be a better example that something I could come up with. 🙂

---

<div class="post-metadata">

**Author:** ![meatinaplasticsack](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/meatinaplasticsack/32/13383_2.png) [@meatinaplasticsack](https://community.postman.com/u/meatinaplasticsack)\
**Post date:** [January 18, 2023, 1:27pm UTC](https://community.postman.com/t/extracting-multiple-values-from-response-header/42309/10 "2023-01-18T13:27:20Z")

</div>

Hello,

@michaelderekjones Could I have additional question with that topic to you (or anyone that can help)? Recently I noticed that I something’s wrong with my solution.

My “location” header in response have value as below:

`let url = "https://qa2.testingstore.com/api/v2.0/upload/process?key=local-d8ae17e1be6348a4a2d06302e3e76131&name=QA2_2023-01-18_13-49_Testing-API_V2.png&expires=2023-01-18T13%3a19%3a14.6912931%2b00%3a00&signature=IR-q7MKOzZk";`

so this function above should:

`pm.environment.set("expires", (getParameterByName("expires", url))); //2023-01-18T13%3a19%3a14.6912931%2b00%3a00`

but instead I am getting this:

`pm.environment.set("expires", (getParameterByName("expires", url))); //2023-01-18T13:19:14.6912931+00:00`

My question is: Why “%3a” is changed to “:” during setting a variable?

---

<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:** [January 18, 2023, 1:51pm UTC](https://community.postman.com/t/extracting-multiple-values-from-response-header/42309/11 "2023-01-18T13:51:45Z")

</div>

@meatinaplasticsack

%3A is the escaped version of :

: is a reserved character so can’t be used in a URL.

[HTTP - URL Encoding (tutorialspoint.com)](https://www.tutorialspoint.com/http/http_url_encoding.htm)

---

<div class="post-metadata">

**Author:** ![meatinaplasticsack](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/meatinaplasticsack/32/13383_2.png) [@meatinaplasticsack](https://community.postman.com/u/meatinaplasticsack)\
**Post date:** [January 18, 2023, 4:21pm UTC](https://community.postman.com/t/extracting-multiple-values-from-response-header/42309/12 "2023-01-18T16:21:10Z")

</div>

Yes, I know that “%3a” is encoded “:” but I did not understand why it was switched.

After some additional verification I know that WHY the value was changed during setting variable. It’s because **decodeURIComponent()** in the function. As a matter of fact it seems that I don’t need decoding anyway and in the end I removed it entirely. Conclusion: always use code tat you fully understand. 😃

**Additional information**

Some interesting implication of this “decoding/encoding”. Before I decided to remove decoding I tried to encode this value again before reusing it in next request. I used this code:

```auto
console.log(`Decoded "expires" variable is: ${pm.environment.get('expires')}`);
let encoded = encodeURIComponent(pm.environment.get("expires"));
pm.environment.set("expires", encoded);
console.log(`Encoded "expires" variable is: ${pm.environment.get('expires')}`);

```

Value without decoding/encoding (this works fine):  
`2023-01-18T16%3a14%3a14.6521723%2b00%3a00`

Value after decoding and encoding it again (this is not working):  
`2023-01-18T16%3A14%3A14.6521723%2B00%3A00`

After encoding it again and putting as a parameter to next request URL, I get 403 forbidden. I manually removed whole decoding/encoding thing from even happening, it works like it should. Why? 🤔 Request URL is not case sensitive, right? 🤨

---

<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:** [January 18, 2023, 5:33pm UTC](https://community.postman.com/t/extracting-multiple-values-from-response-header/42309/13 "2023-01-18T17:33:37Z")

</div>

Sorry, not sure why its doing this.

Both of those values seem to return the same value when decoded which should be the deciding factor when the request is sent.

I’ve looked at the collectionVariable and the string looks ok here.

```auto
console.log(decodeURIComponent("2023-01-18T16%3a14%3a14.6521723%2b00%3a00"));
console.log(decodeURIComponent("2023-01-18T16%3A14%3A14.6521723%2B00%3A00"));

```

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

[Are Domains & URLs Case Sensitive? (9 Important Facts) (techpenny.com)](https://techpenny.com/are-domains-urls-case-sensitive/)

---

<div class="post-metadata">

**Author:** ![meatinaplasticsack](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/meatinaplasticsack/32/13383_2.png) [@meatinaplasticsack](https://community.postman.com/u/meatinaplasticsack)\
**Post date:** [January 18, 2023, 8:45pm UTC](https://community.postman.com/t/extracting-multiple-values-from-response-header/42309/14 "2023-01-18T20:45:14Z")

</div>

Well, maybe at some point will be able to explain that weird behavior. Anyway, thank you @michaelderekjones for all your help! You really saved me with that function. 😉
