How do i set up a bearer token in postman from an environment variable?
Asked Answered
D

6

13

I have set up a collection in PostMan and am able to save my bearer token value to an environment variable successfully using the following test

var jsonData = JSON.parse(responseBody);
pm.environment.set("mytoken", jsonData.token);

but how do I set up a new call to use it?

I have tried adding a Header with

Authorization Bearer <mytoken>

but when I Post the Status is 401 Unauthorized

Dihedral answered 17/6, 2018 at 5:23 Comment(1)
Hey, You can also now just use pm.response.json() instead of JSON.parse(responseBody)Eructate
D
7

In the headers I needed to use

for the key

Authorization 

for the value

Bearer {{mytoken}}
Dihedral answered 17/6, 2018 at 5:51 Comment(1)
I missed answering this question back when I asked it. So just ticket it now. Other answers here may be better.Dihedral
P
21

You can use Tests tab to write your code which updates the Environment variable, as explained in this link. Read more about Test scripts here.

enter image description here

Assuming the response of the auth call is:

{
    "token": "woaejrlajfaoidhfalskdjfalsdijfasd"
}

Then, in Tests tab, you can write like:

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("token", jsonData.token);

This will update the variable token whenever you trigger the auth call. This token variable should be used in headers of all the API calls, to update automatically.

Do also check inheriting the auth.

Permanence answered 20/1, 2020 at 23:36 Comment(1)
Note that postman.setEnvironmentVariable is deprecated, for me it did not work at all with the latest Postman v7.18.0. Use pm.environment.set instead.Cayla
R
8

I use a script after login post into tests tab like below;

let jsonData = JSON.parse(responseBody);

pm.collectionVariables.set("jwt_token", jsonData.data.token);

enter image description here

and create a collection variable like following; enter image description here

Repetition answered 26/3, 2021 at 11:29 Comment(0)
D
7

In the headers I needed to use

for the key

Authorization 

for the value

Bearer {{mytoken}}
Dihedral answered 17/6, 2018 at 5:51 Comment(1)
I missed answering this question back when I asked it. So just ticket it now. Other answers here may be better.Dihedral
J
7

Summary:

  1. Create a variable to store Auth Token value in single place to use throughout your collection.
  2. Set default method for Authorization for your entire collection.
  3. Instead of setting the Authorization header for each request set the Authorization on each request to use "Inherit auth from parent" to automatically populate the request with the proper auth headers.

You can define variables in Postman environments and collections in order to simplify your requests by setting a value in one place and reference it in as many places as necessary. So you can create a variable for your Bearer Token value. Do this by editing your collection and going to the Variables tab to add a new variable.

While editing your collection go to the Variables tab to add a new variable you can use throughout your collection.

Also while editing your collection go the Authorization tab to set a default authorization for all requests within your collection. You can set the Authorization Type for your collection to Bearer and set the Token value to be your defined variable. This will allow you to use the same authorization token for all of your requests within your collection:

Also while editing your collection go the Authorization tab to set a default authorization for all requests within your collection.

Then in order to use the collection's default method of authorization, you will need to set the requests within that collection to set the Authorization Type to "Inherit auth from parent". Doing this will allow you to not have to deal with adding the Authorization header manually on to each request. Each request within the collection with the "Inherit auth from parent" authorization type selected will automatically populate the request with the proper headers for authorization if you have defined a default option for the collection like in the previous image.

Set each request to use the Authorization Type "Inherit auth from parent".

Cheers!

Jac answered 6/6, 2019 at 17:30 Comment(0)
L
2
pm.environment.set("JWT",pm.response.json().token)

Note : JWT is the environment variable you set in your environment

Leinster answered 28/4, 2021 at 16:55 Comment(0)
C
1

Like the way Kristen, said. Or else download latest postman desktop application, in that in authorization they have an option to add bearer token in the header

Chewning answered 17/6, 2018 at 6:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.