Set HTTP header for every Postman request

To add or modify a HTTP header for each request contained in a Postman collection or folder you can use the “Pre-request Script” feature. Hereby you can execute JavaScript to manipulate the environment before a request is being executed.

Postman "Pre-request Script"
Postman “Pre-request Script”

You can access the “Pre-request Script” tab by navigating to the desired collection or folder in the left hand navigation tree.

In my example I wanted to make sure that the Accept-Language header is being set or updated to the current value of my global variable named language.

var language = pm.globals.get("language");
pm.request.upsertHeader({ key: 'Accept-Language', value: language});

You could also access an environment scoped variable with pm.environment.get("variable_key"); to access a specific API key for example.

To ensure that the requests are being manipulated the way you want to you can ensure that by using the Postman console. You can open the console by clicking on the button in the bottom left corner.

Examination of the HTTP request in the Postman console
Examination of the HTTP request in the Postman console

Leave a Reply

Your email address will not be published. Required fields are marked *

twelve + eighteen =