Authorization type Bearer Token on Postman

13,592

At first, create an environment ( top right corner of postman - image below ) This is not a mandatory step by I suggest you to do for better handling of variables

enter image description here

I have modified the script to suit your need

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("ID", jsonData.Location.split("?token=")[1]);

Now this will export the value of the token ( screenshot below )

enter image description here

All you have to do next is to call the variable in request #2

enter image description here

By this you don't have to manually copy, paste into request #2 every single time

Share:
13,592
code-8
Author by

code-8

I'm B, I'm a cyb3r-full-stack-web-developer. I love anything that is related to web design/development/security, and I've been in the field for about ~9+ years. I do freelance on the side, if you need a web project done, message me. ;)

Updated on June 05, 2022

Comments

  • code-8
    code-8 over 1 year

    I'm trying test a few endpoints using Postman.

    All endpoint, require a token which can be obtain by log-in.

    So I did this :

    Request #1

    enter image description here

    After login success, I have access to the token from the response, then I store that token in my global variable.

    let token = pm.response.json().location
    console.log('Token : ', token.split("?token=")[1]);
    pm.globals.set("token", token)
    

    I need to use that token as Authorization type Bearer Token for my request #2.

    enter pastedescription here

    I can copy & paste that in the token box, but I tried to avoid doing that manually, is there a way to do it automatically so I can run these 2 requests in sequence?

  • Danny Dainton
    Danny Dainton about 5 years
    So basically my comment ;) You dont need to create an environment file as it's already getting set as a global variable. I would also suggest moving away from the old style syntax and use the pm. * api.
  • Wilfred Clement
    Wilfred Clement about 5 years
    I'm still on the chrome extension so the "postman" syntax, also the reason for the environment is because in my postman I have 2 API's that generates token and i have to use the same variable ( project demand ) so I map it against different environments
  • Danny Dainton
    Danny Dainton about 5 years
    I would migrate to the stand alone version of Postman, the chrome extension is not even actively worked on anymore. It's a major version behind the native application now.