# Retrieve query param value from Location Header in response

**URL:** <https://community.postman.com/t/retrieve-query-param-value-from-location-header-in-response/21148>\
**Category:** Help Hub\
**Tags:** code\
**Created:** [March 9, 2021, 4:13am UTC](https://community.postman.com/t/retrieve-query-param-value-from-location-header-in-response/21148 "2021-03-09T04:13:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![krishnakumar.tce](https://avatars.discourse-cdn.com/v4/letter/k/2acd7d/32.png) [@krishnakumar.tce](https://community.postman.com/u/krishnakumar.tce)\
**Post date:** [March 9, 2021, 4:13am UTC](https://community.postman.com/t/retrieve-query-param-value-from-location-header-in-response/21148/1 "2021-03-09T04:13:45Z")

</div>

I have a 3-legged OAuth setup.  
The first step returns a redirect with a Location header that has the authorization token (code) as a query parameter.  
This header looks like follows -  
Location: [https://b7af580a-bc88-4ab7-b9ad-bf367ad823d0.mock.pstmn.io?code=fGTxeQG8&scope=actionShipmentCreate%20actionShipmentEdit](https://b7af580a-bc88-4ab7-b9ad-bf367ad823d0.mock.pstmn.io?code=fGTxeQG8&scope=actionShipmentCreate%20actionShipmentEdit)

I want to extract the value of the query param “code” whose value is “fGTxeQG8” so I can save it in a collection variable and use it in the next step for getting an access token.

How do I do this?  
Since postman doesn’t recognize this as a URL object, none of the standard methods are working, such as what works on the request URL itself, which postman understands as a URL object and parses easily.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 9, 2021, 11:03am UTC](https://community.postman.com/t/retrieve-query-param-value-from-location-header-in-response/21148/2 "2021-03-09T11:03:22Z")

</div>

Hey @krishnakumar.tce 👋

A long way to go about it but this could work in the `Tests` tab:

```auto
const querystring = require('querystring');

let params = pm.request.headers.get('location').split('?')

console.log(querystring.parse(params[1]).code)

```

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/0/608d95034f4562c349bda47c6e78fa01ea7aaefe.png)

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [March 9, 2021, 12:57pm UTC](https://community.postman.com/t/retrieve-query-param-value-from-location-header-in-response/21148/3 "2021-03-09T12:57:03Z")

</div>

`console.info(pm.variables.replaceIn(pm.request.url.toJSON()).query.find(a=>a.key==="code")["value"])`

you can use this in pre-request section or test section

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 9, 2021, 2:13pm UTC](https://community.postman.com/t/retrieve-query-param-value-from-location-header-in-response/21148/4 "2021-03-09T14:13:36Z")

</div>

The value is going to be in the `Location` header of the redirected response - Not really sure how that code would work.

Also, not sure what the `pm.variables.replaceIn()` is doing there 🤔

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [March 9, 2021, 2:41pm UTC](https://community.postman.com/t/retrieve-query-param-value-from-location-header-in-response/21148/5 "2021-03-09T14:41:39Z")

</div>

Thanks @dannydainton ,

I thought the user wants it from the url that he uses , used replaceIN to resolve any variables in prerequest ,

Could use postman-collection to parse url

```
parser = require('postman-collection').Url.parse

console.log(parser(pm.request.headers.get('location')).query.find(a=>a.key==="code")["value"])
```

---

<div class="post-metadata">

**Author:** ![krishnakumar.tce](https://avatars.discourse-cdn.com/v4/letter/k/2acd7d/32.png) [@krishnakumar.tce](https://community.postman.com/u/krishnakumar.tce)\
**Post date:** [March 9, 2021, 3:18pm UTC](https://community.postman.com/t/retrieve-query-param-value-from-location-header-in-response/21148/6 "2021-03-09T15:18:17Z")

</div>

worked like a charm! thanks.
