前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Other】Extracting Data from Responses and Chaining Requests

【Other】Extracting Data from Responses and Chaining Requests

作者头像
阿东
修改2023-09-08 22:44:40
970
修改2023-09-08 22:44:40
举报
文章被收录于专栏:公众号:懒时小窝

原文

https://blog.postman.com/extracting-data-from-responses-and-chaining-requests/

正文部分

Postman lets you write scripts that run before/after you receive a response from the server. You can do practically anything in these scripts. The pre-request and test scripts run inside a sandbox and Postman enable the Postman object to interact with the main Postman context.

PostMan 可以让你编写脚本,运行在收到一个响应之前或者之后。

This opens up a number of new possibilities.

这带来了许多新的可能性。

One of them is extracting values from the response and saving it inside an environment or a global variable.

其中之一就是从响应中提取值,并将其保存在 environment全局变量中。

Environment and global variables let you keep track of everything that affects the APIs state.

环境变量和全局 变量 可以让您跟踪影响APIs 状态的所有内容。

Some examples of common variables you would use with an API are session tokens and user IDs.

在 API 中使用的常见变量包括会话令牌和用户 ID。

The flow while working with variables currently goes like this:

使用变量](https://blog.postman.com/10-tips-for-working-with-postman-variables/) 时的流程目前是这样的:

  1. Send a request from Postman
  2. Receive the response and select and copy a value from the response body or the header
  3. Go to the environment manager
  4. Set the variable value
  5. Hit submit
  6. 从 Postman 发送请求
  7. 接收响应,从响应体或响应头中选择并复制一个值
  8. 进入环境管理器
  9. 设置变量值
  10. 点击提交

This works, but is a lot of work if you have more than a few variables. Test scripts dramatically simplify this flow. All you have to do is call postman.setEnvironmentVariable(key, value) or postman.setGlobalVariable(key, value) to set a variable with values you have extracted from the response. You can even add something dynamically generated through Javascript.

这种方法可行,但如果变量较多,则工作量很大。 测试脚本 大幅简化了这一流程。您只需调用postman.setEnvironmentVariable(key, value)postman.setGlobalVariable(key, value) ,就能用从响应中提取的值设置变量,甚至可以添加通过 Javascript 动态生成的内容。

Lets go through an example which will illustrate this in more detail:

让我们通过一个例子来详细说明这一点:

1. Create and select an environment 创建和选择环境

For this example, we’ll create and set a blank test environment.

在本例中,我们将创建并设置一个空白的测试环境。

2. GET request to get response body 通过 GET 请求获取响应正文

This request returns a JSON body with a session token. For this dummy API, the token is needed for a successful POST request on the ‘/status’ endpoint. To extract the token, we need the following code.

该请求会返回一个带有会话标记的 JSON 主体。对于这个虚拟应用程序接口来说,在"/status "端点上成功发送 POST 请求时需要使用令牌。要提取令牌,我们需要以下代码。

代码语言:javascript
复制
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("token", jsonData.token);

Add this to the test editor and hit send. Hover over the quick look window (q) to check that the variable “token” has the value extracted from the response

将此添加到测试编辑器中,然后点击发送。将鼠标悬停在快速查看窗口 (q) 上,检查变量 "token "是否具有从响应中提取的值

3. POST request with the previous session token 使用前一个会话标记的 POST 请求

To send the token, we need to set it as part of the POST request. Let’s add a form-data variable to the ‘/status’ request.

要发送令牌,我们需要将其设置为 POST 请求的一部分。让我们在"/status "请求中添加一个 form-data 变量。

On hitting send, Postman sends the token along with the request.

点击发送后,Postman 会将令牌与请求一起发送。

Test scripts let you extract variables and chain together requests in any way you like. As request variables work everywhere, you can build a sequence of API calls which exactly mirror your use case.

通过测试脚本,您可以提取变量,并以任何方式将请求串联起来。由于请求变量在任何地方都能发挥作用,因此您可以建立一个 API 调用序列,完全反映您的使用情况。

本文系外文翻译,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系外文翻译前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 原文
  • 正文部分
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档