Add headers params to environment

Is that possible to add headers parameters for every calls make from an environment?

I explain.

If I select my custom environment, which has some custom parameters setted in header part. Then, when I make a call in cUrl, PHP, Shell…, Postman add automatically environment headers in calls.

1 Like

You can add headers like params and put in your variable values.

Yep, but I mean add it automatically instead of setting from us for each our API calls.
This could be great, especially if you have a lot of API for one environment :slight_smile: .

1 Like

Hi @caudurieau,

You can save commonly used headers together in a header preset. Under the Headers tab, you can add a header preset to your request when you select “Manage Presets” from the Presets dropdown on the right. You can go ahead and apply those directly instead of manually adding it for each request. please view the following documentation for your reference:
https://learning.getpostman.com/docs/postman/sending_api_requests/requests/#header-presets

1 Like

That’s not exactly what I suggest but I didn’t know this functionality. Thanks :wink:

Hey @caudurieau

Is this what you’re trying to do?

Using the pm.request.headers.add({key: 'header_name', value: 'header_value' }) function in the Pre-Request Script at the Collection or Folder level, you would be able to programmatically set a header in one place that would be applied to multiple requests.

4 Likes

Hi @caudurieau audurieau I just encounter the same problem you had and write this solution (on pre request script):

var env = pm.environment.get("path");
if (env === "local" || "dev")  {
    pm.request.headers.remove('Authorization')
    pm.request.headers.add({key: 'Authorization', value: pm.globals.get("devJWT") })
}
else {
    pm.request.headers.remove('Authorization')
    pm.request.headers.add({key: 'Authorization', value: pm.globals.get("prodJWT") })
}
2 Likes