我的应用程序 API 需要通过身份验证令牌进行身份验证。简而言之,我们向/authentication
端点,它使用包含令牌的 JSON 对象进行响应,例如:
{"token": "xxxxxxxxxxxxxxxxxxxxxx"}
我们应用程序中的每个其他 API 端点都需要authentication
包含此令牌的标头。现在,在 Postman 中可以执行身份验证请求,复制令牌,打开下一个端点并粘贴authentication
手动输入标题。但当测试大量端点时,这会变得乏味且耗时。
有没有办法让 Postman 保存并自动添加authentication
任何后续请求中的一个请求的令牌?
更好的是,邮递员可以自动发送/authentication
在任何其他请求之前请求?
Postman 在创建 API 请求时为您提供了多种选择。
在您的情况下,您可以在收到令牌时通过以下方式为其创建一个全局变量:
var jsonData = JSON.parse(responseBody);
postman.setGlobalVariable('token', jsonData.token);
这会进入你的Tests https://www.getpostman.com/docs/postman/scripts/test_scripts选项卡,以便在您的请求完成后执行此脚本。
现在,一个全局变量token
已设置并可以使用{{token}}
您发出的以下 API 请求中的语法。
我将用一个类似的例子向您展示同样的内容:
1. Save the data of latitude and longitude into the global variables lat
and long
.
2. Reuse the data by referring to the name of the variable, i.e. lat
and long
by enclosing them within curly braces like {{lat}}
and {{long}}
.
- 您还可以通过单击右上角的齿轮图标并选择管理环境然后打开“全局”选项卡来管理这些全局变量。
- 提示:您还可以将获取令牌的请求保存到集合中,这样每次都不必编写 URL 来获取令牌。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)