I have a Postman pre-request script which uses pm.sendRequest to call an endpoint that uses the same authorisation as the current request. I tried to use pm.request.headers in the script to get the Authorization header so I could add the same header in the pm.sendRequest call, but it doesn’t return the auto-generated headers, only the headers I’ve set manually. Is there any way to access the auto-generated headers in a pre-request script?
I tried
header: {
"Authorization": pm.request.headers.get("Authorization"),
"Content-Type": "application/json"
},
I do not know how to access the auto-generated headers, but you can actually access the data from the Authorization tab of Postman and thus also the access token:
pm.request.auth.parameters().get("accessToken")
I use this for setting the token parameter to a Postman variable named token_param when hitting the OIDC token introspection endpoint after having authenticated via OAuth 2.0 Authorization Code Flow in a browser tab from Postman:
pm.variables.set('token_param', pm.request.auth.parameters().get("accessToken"))
You can also check what's inside the auth object by calling the toJSON method:
console.info(pm.request.auth.toJSON())
That way I found the accessToken:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With