Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get auto-generated headers with pm.request.headers in Postman?

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"
},
like image 475
AndrWeisR Avatar asked Oct 26 '25 11:10

AndrWeisR


1 Answers

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:

accessToken in auth object

like image 67
devrys Avatar answered Oct 28 '25 03:10

devrys