Authorization type Bearer Token on Postman
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
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 )
All you have to do next is to call the variable in request #2
By this you don't have to manually copy, paste into request #2 every single time
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, 2022Comments
-
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
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.
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 about 5 yearsSo 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 about 5 yearsI'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 about 5 yearsI 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.