原文
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...) 时的流程目前是这样的:
- Send a request from Postman
- Receive the response and select and copy a value from the response body or the header
- Go to the environment manager
- Set the variable value
- Hit submit
- 从 Postman 发送请求
- 接收响应,从响应体或响应头中选择并复制一个值
- 进入环境管理器
- 设置变量值
- 点击提交
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账户,了解更多技巧和窍门。