在TestRigor中如何提取API响应ID字段用于后续请求?
TestRigor中API响应字段提取失败的解决方法及用例修正
常见问题原因及修正方案
JsonPath语法使用错误
TestRigor提取JSON字段需要标准的JsonPath表达式,直接写字段名(比如record_id)无法被识别,必须加上根路径前缀$。如果record_id是响应JSON的顶级字段,正确的表达式应为$.record_id;如果是嵌套结构(比如{"data": {"record_id": "xxx"}}),则需写成$.data.record_id。PUT请求中变量引用格式错误
在TestRigor的请求体中引用变量,需要用${变量名}的格式,而非直接写变量名字符串。
修正后的测试用例
//Step 1: 创建record_id并保存变量 call api "<API_URL>" with headers "a:a" and "b:b" and body "body" and get "$.record_id" and save it as "var_record_id" //Step 2: 验证变量是否成功保存(可选步骤,用于排查问题) check that variable "var_record_id" is not empty //Step 3: 更新指定record_id为允许状态 call api "<API_URL>" with headers "a:a" and "b:b" and body from the string with parameters text starting from next line and ending with [END] { "param": "${var_record_id}", "state": "permit" } [END]
额外排查步骤
- 确认POST请求的响应是标准JSON格式,可通过查看响应头
Content-Type是否为application/json来验证 - 打印POST响应内容辅助排查:添加步骤
print api response,执行后查看输出的JSON结构是否符合预期
内容的提问来源于stack exchange,提问作者Myles
相关产品推荐
相关产品推荐

