gpt4 book ai didi

authentication - 在 Postman 中的后续请求中使用身份验证 token

转载 作者:行者123 更新时间:2023-12-01 17:05:31 24 4
gpt4 key购买 nike

我的应用程序 API 需要通过身份验证 token 进行身份验证。简而言之,我们向 /authentication 端点发送请求,它会使用包含 token 的 JSON 对象进行响应,例如:

 {"token": "xxxxxxxxxxxxxxxxxxxxxx"}

我们应用程序中的每个其他 API 端点都需要包含此 token 的身份验证 header 。现在,在 Postman 中,可以执行身份验证请求、复制 token 、打开下一个端点并手动粘贴 authentication header 。但当测试大量端点时,这会变得乏味且耗时。

有没有办法让 Postman 保存并自动在任何后续请求中添加一个请求中的身份验证 token ?

更好的是,Postman 可以在任何其他请求之前自动发送 /authentication 请求吗?

最佳答案

Postman 允许您在制作 API 请求时有多种选择。

就您的情况而言,您可以在收到 token 时通过以下方式为其创建一个全局变量:

var jsonData = JSON.parse(responseBody);
postman.setGlobalVariable('token', jsonData.token);

这将出现在您的 Tests 中选项卡,以便在您的请求完成后执行此脚本。

现在,全局变量 token 已设置,并且可以在您发出的以下 API 请求中使用 {{token}} 语法进行访问。

我将通过一个类似的示例向您演示相同的内容:

1. 将经纬度数据保存到全局变量latlong中。 2. 通过引用变量的名称来重用数据,即 latlong,将它们括在大括号内,如 {{lat}}{{long}}

  1. You can also manage these global variables, by clicking on the gear icon in the top right corner, and selecting manage environments then opening the Globals tab.
  2. Tip: You can also, save the request to obtain the token into your collections, so that each time, you don't have to craft the URL to obtain the token.

关于authentication - 在 Postman 中的后续请求中使用身份验证 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45362308/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com