问题描述
我正在做一些加特林行动,由于我从不做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
)
不是我做的更干净的代码,但是它可以工作。