Pregunta
I have different environments for an api (different servers, dev, staging, live). I can use curly brace placeholders for say the username, but I still need to replace the auth headers on each stored request.
Is it possible to run a post environment script that automatically updates headers or does what ever needs to be done?
Comment: I can use the generated header value from the environment, that is something like Basic blabla
which is then referenced in the requests. Still wondering whether a more elegant way is possible
Solución
Hi I think I have a similar setup with my authorization headers.
In postman there is a button "Manage presets" which allows you to set default header values that can be used across different requests. I've created a parameterized header in the following way:
Key: Authorization, Value = Bearer {{Access_token}}
At the top center there is a section where you can manage variable in your environments. For each environment I have created the following variable to hold the access token:
Key: Access_token, Value =
Note the value is left blank as it will be set after the authorization response is generated in the next step.
I then use the Test editor because it runs AFTER the response is generated as opposed to the Pre-request Script editor which runs before. In the Test editor I grab the value of my authentication token with the following script:
var data = JSON.parse(responseBody);
postman.setEnvironmentVariable("Access_token", data.Access_token);
Note that I'm setting this variable per environment (not globally) so that I can have separate tokens for each environment (Dev, QA, Staging, Production, etc).
So now the token that I captured can be sent as a header with each subsequent request by Clicking the "Add preset" button and choosing the Authorization preset header I previously defined.
So for my needs the flow goes as follows
- Request to get access code
- With response from access code, Reqeust is made to get token
- Token is saved as an environment variable.
- On each subsequent call the authorization header is passed as a preset header automatically.