3

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
code-8
  • 54,650
  • 106
  • 352
  • 604
  • 1
    You're setting the token as a global variable right? Can you not just use `Bearer {{token}}` in an Auth header on the second request? That Token input is only creating the same header. – Danny Dainton Aug 23 '18 at 21:12
  • ohh smart, I didn't think of that. – code-8 Aug 24 '18 at 14:17

2 Answers2

6

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

Wilfred Clement
  • 2,674
  • 2
  • 14
  • 29
  • 1
    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. – Danny Dainton Aug 24 '18 at 09:45
  • 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 – Wilfred Clement Aug 24 '18 at 10:05
  • 1
    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. – Danny Dainton Aug 24 '18 at 11:45
0

NO there isn't any till now. It has to be done manually if you want to have complete value or else you can store it in a variable and use that variable directly for the token.