Authorization type Bearer Token on Postman

只谈情不闲聊 提交于 2019-12-13 04:22:31

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/51992982/authorization-type-bearer-token-on-postman

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!