How to add authorization header in POSTMAN environment?
Asked Answered
L

6

59

I'm testing bunch of API calls using POSTMAN. Instead of adding authorization header to each request, can I make it as a part of POSTMAN environment? So, I don't have to pass it with every request.

Linnealinnean answered 11/11, 2016 at 0:57 Comment(1)
I have a posted simpler solution here which works for all cases https://mcmap.net/q/328301/-how-to-add-authorization-header-in-postman-environmentPelt
M
61

Yes, you can do this through Postman by assigning your header as an environment variable, let's say authorization, as follow:

Authorization header

then set you environment variable with its value as follow:

Environment variable

Magdalenmagdalena answered 13/11, 2016 at 17:47 Comment(2)
^ Agree with above comment, I came here looking for how to add a header to every request without having to edit each request manually. I have 58 requests in the suite I'm currently working on, and a new requirement that each one send an Authorzation header that wasn't present before.Hibbs
This requires two steps. I tried this approach which is a single step for headers for all existing and new requests in a collection https://mcmap.net/q/328301/-how-to-add-authorization-header-in-postman-environmentPelt
I
22

In contemporary releases of Postman, you can just set your auth on the collection (or folder), and have every request inherit it (which I believe new requests do by default).

Edit collection menu

Set your auth on the authorization tab

Indus answered 20/2, 2019 at 21:19 Comment(4)
How do you add this after the collection has already been created? I dont find a way to edit the collection and add auth. It only appears to exist when you first create a collection.Freemasonry
Updated with screenshots showing how to do this in the Postman UIIndus
This is what I needed. ThanksHon
Also, this, if you want to set custom Auth header(s), instead of the normal, selectable ones: postman.com/postman/workspace/postman-answers/collection/…Mistress
I
5

postman usually remembers your key-value pairs you send in header. So there is no need to add headers each request. Anyway you can configure a "Preset" with your auth token. enter image description here

Iveson answered 11/11, 2016 at 1:23 Comment(1)
yea but what if you need different preset to each environmentChairman
I
1

Not sure if this is what you're looking for, but we use a link-based API that requires auth headers on each request. If you go to Postman > Preferences > General and enable Retain headers when clicking on links, Postman will pass through your auth headers to the child links.

Hope that helps!

Interstice answered 26/2, 2018 at 22:55 Comment(0)
C
0

If you can't wait here is a work around I just made:

  1. Export your collection (data format v2.1)
  2. Open firefox , dev tools, scratch pad
  3. Paste the code below
  4. Replace the header information with your header
  5. Replace the var a with your contents of the exported .json file
  6. Run the script
  7. The copy(b) command will put the new data with in your clipboard
  8. In postman, click import > Paste Raw Text > Import > as a copy.
  9. Verify your requests have your header, and run it :)

var myHeader = {
  "key": "X-Client-DN",
  "value": "{{Postman-DN}}",
  "description": "The User's DN Interacting with the system."
};

function addHeader(obj, header) {
  if (obj.hasOwnProperty('request')) {
    obj.request.header.push(myHeader)
  }
  if (obj.hasOwnProperty('item')) {
    obj.item.forEach(function(element) {
      element = addHeader(element, header);
    });
  }
  return obj;
}

var a = {
  "item": [{}, {
    "request": {
      "header": []
    }
  }, {
    "item": [{
      "request": {
        "header": []
      }
    }]
  }]
}

var b = addHeader(a, myHeader);
console.log(JSON.stringify(b, null, 2))

// Might have to run copy manually on console
//copy(b);
Circinus answered 19/10, 2017 at 11:7 Comment(0)
P
0

A much simpler way is to use API Key auth type for a collection. It works for existing as well as new collections.

It lets you specify custom key and value which can be used as header as well as query params. Screenshot below: api key auth for custom auth headers

Pelt answered 11/7 at 16:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.