3

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"
},
devrys
  • 1,571
  • 3
  • 27
  • 43
AndrWeisR
  • 1,110
  • 11
  • 21

1 Answers1

1

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

devrys
  • 1,571
  • 3
  • 27
  • 43
  • 2
    It's not working for me because my auth is inherited from the parent. 'pm.request.auth` is `undefined`. – AndrWeisR Sep 15 '22 at 05:47