邮递员上的授权类型不记名令牌 [英] 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.
我可以复制&将其粘贴到令牌框中,但我尝试避免手动执行此操作,有没有一种方法可以自动执行此操作,以便可以依次运行这两个请求?
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中
这篇关于邮递员上的授权类型不记名令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!