我有一段代码需要遍历从 REST 服务的响应中获得的 JSON 数组。(这里有完整的要点。)
.exec(http("Request_1")
.post("/endPoint")
.headers(headers_1)
.body(StringBody("""REQUEST_BODY""")).asJSON
.check(jsonPath("$.result").is("SUCCESS"))
.check(jsonPath("$.data[*]").findAll.saveAs("pList")))
.exec(session => {
println(session)
session
})
.foreach("${pList}", "player"){
exec(session => {
val playerId = JsonPath.query("$.playerId", "${player}")
session.set("playerId", playerId)
})
.exec(http("Request_1")
.post("/endPoint")
.headers(headers_1)
.body(StringBody("""{"playerId":"${playerId}"}""")).asJSON
.check(jsonPath("$.result").is("SUCCESS")))
}
第一个请求的响应格式是
{
"result": "SUCCESS",
"data": [
{
"playerId": 2
},
{
"playerId": 3
},
{
"playerId": 4
}
]
}
并playerId
在会话中显示为
pList -> Vector({playerId=2, score=200}, {playerId=3, score=200}
我在第二个请求中看到正文是
{"playerId":"Right(empty iterator)}
预期:3 个请求,正文为
{"playerId":1}
{"playerId":2}
{"playerId":3}
如果我只保存 playerIds,我可以成功循环结果数组:
.check(jsonPath("$.data[*].playerId").findAll.saveAs("pList")))