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
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
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('?')
console.log(querystring.parse(params[1]).code)
2 Likes
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
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
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
console.log(parser(pm.request.headers.get('location')).query.find(a=>a.key==="code")["value"])
worked like a charm! thanks.
1 Like