ApiPost自动化测试基础之:接口参数依赖的情景处理
ApiPost V3版本的参数依赖处理方式同旧版雷同。
什么是接口参数依赖:
接口参数依赖又称作接口依赖,简单点说就是后面的接口要用到前面的接口产生的数据。
比如:我们一个接口B需要A接口返回的参数token作为自己的请求参数。常见的场景如:访问一个需要登陆才能浏览的接口。
ApiPost如何处理参数依赖?
为了便于演示,首先,我们准备2个接口:
get_token接口:
请求地址:{{url}}get_token.php
它将返回一个token参数。如图:

need_token接口:
请求地址:{{url}}need_token.php
它需要get_token接口返回的token参数作为自己的请求参数。

如上图所示,我们定义了一个环境变量token_var,由于此时尚未将其与get_token接口返回的token绑定,所以此时它原样输出了。
绑定get_token的响应结果token到环境变量token_var
此时点击get_token接口的【后执行脚本】,通过一个变量赋值,就可以将get_token的响应结果token到环境变量token_var。

此时,点击【发送】按钮,就可以把get_token接口返回的token绑定到环境变量token_var了。
我们进入need_token的接口,并点击发送,就会看到token已经是我们刚才获取到的token了。

注意:我们绑定完成后,我们需要先再次发送请求下get_token接口(执行绑定数据),再请求need_token接口。
附:响应类型对应关系:
response.raw:原始响应数据
调用示例:
response.raw.status //响应状态码(200、301、404等)
response.raw.responseTime //响应时间(毫秒)
response.raw.type //响应类型(json等)
response.raw.responseText //响应文本
response.json :json格式的响应数据
调用示例如上面示例:
response.json.data.token //也可以 response.json.data["token"]
response.headers :响应头

调用示例:
response.headers.server //也可以 response.headers["server"]
response.cookies :响应cookie
调用示例:
response.cookies.PHPSESSION //也可以 response.cookies["PHPSESSION"]