Postman 上的授权类型 Bearer Token [英] Authorization type Bearer Token on Postman
问题描述
我正在尝试使用 Postman 测试一些端点.
I'm trying test a few endpoints using Postman.
所有端点,需要一个可以通过登录获得的令牌.
All endpoint, require a token which can be obtain by log-in.
所以我这样做了:
请求 #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)
我需要将该令牌用作我的请求 #2 的授权类型承载令牌.
I need to use that token as Authorization type Bearer Token for my request #2.
我可以复制 &将其粘贴到令牌框中,但我试图避免手动执行此操作,有没有办法自动执行此操作,以便我可以按顺序运行这 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?
推荐答案
首先,创建一个环境(邮递员右上角-下图)这个不是我建议您为更好地处理变量而做的强制性步骤
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 )
接下来你要做的就是调用请求#2中的变量
All you have to do next is to call the variable in request #2
这样您就不必每次都手动复制、粘贴到请求 #2 中
这篇关于Postman 上的授权类型 Bearer Token的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!