关于postman:OtherExtracting-Data-from-Responses-and-Chaining-Requests

84次阅读

共计 3154 个字符,预计需要花费 8 分钟才能阅读完成。

原文

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-var…) 时的流程目前是这样的:

  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 申请时须要应用令牌。要提取令牌,咱们须要以下代码。

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 调用序列,齐全反映您的应用状况。

You can download the sample collection and import it inside Postman. Check out the docs for more Postman features. There are more tutorials planned in this series. Keep a tab on this blog as well as the Twitter account for more tips and tricks.

您能够下载 sample collection 并将其导入 Postman。查看 docs 理解更多 Postman 性能。本系列还有更多教程。请随时关注本博客和 Twitter 账户,理解更多技巧和诀窍。

正文完
 0