Retrieve query param value from Location Header in response

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 -

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.

Hey @krishnakumar.tce :wave:

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

const querystring = require('querystring');

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


2 Likes>a.key==="code")["value"])

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

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 :thinking:

1 Like

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


worked like a charm! thanks.

1 Like