我正在做一些加特林,因为我从来没有做过scala,所以我有点迷失了。 我想在发回之前修改收到的 JsonPath 的 JSON 响应
我的代码看起来像这样
.exec(
http("Get call")
.get("getEndpoint")
.check(jsonPath("$.value").saveAs("RESPONSE_DATA"))
)
.exec(
http("Post call")
.post("postEndpoint")
.header("content-type", "application/json")
.body(StringBody("${RESPONSE_DATA}"))
.asJson
)
例如,我想更改为从 Get Call 中以 Json 形式收到的用户的名字。我无法找到加特林文档的答案
感谢 Lars 的评论,我设法找到了解决方案。我太专注于寻找加特林的具体方法而忘记了编程的基本方法
这是新代码
.exec(
http("Get call")
.get("getEndpoint")
.check(jsonPath("$.value").saveAs("RESPONSE_DATA"))
)
.exec(session =>
{
// put body response into variable
val response = session("RESPONSE_DATA").as[String];
// generate random string as you convenience
val randomString = Random.alphanumeric.filter(_.isLetter).take(5).mkString;
// use replace method to modify your json (which is right now a string)
newResponse = response.replace(
"""specificKey":""",
"""specificKey":""" + randomString + "",
)
session
}.set("RESPONSE_DATA", newResponse)
// ^ really important to set the new value of session outside of brackets !!
)
.exec(
http("Post call")
.post("postEndpoint")
.header("content-type", "application/json")
.body(StringBody("${RESPONSE_DATA}"))
.asJson
)