Parsing a Json response returned to Gatling

匿名 (未验证) 提交于 2019-12-03 03:06:01

问题:

I am trying to parse a json response returned to gatling by the server.

My response from server is:

SessionAttribute(   Session(     GetServices,     3491823964710285818-0,     Map(       gatling.http.cache.etagStore -> Map(https://api.xyz.com/services -> ),        gatling.http.cache.lastModifiedStore -> Map(https://api.xyz.com/services -> ),       myresponse -> {         "created":"2014-12-16T22:06:59.149+0000",         "id":"x8utwb2unq8uey23vpj64t65",         "name":"myservice",         "updated":"2014-12-16T22:06:59.149+0000",         "version":null       }),     1418767654142,622,     OK,List(),<function1>),id) 

I am doing this in my script:

val scn = scenario("GetServices")           .exec(http("Get all Services")           .post("/services")           .body(StringBody("""{ "name": "myservice" }""")).asJSON           .headers(sentHeaders)           .check(jsonPath("$")           .saveAs("myresponse")) ).exec(session => {   println(session.get("id"))   session }) 

It is still printing the whole response. How can I just retrieve the id which is "x8utwb2unq8uey23vpj64t65"?

回答1:

It might be easiest to use a little bit more jsonPath to pull out the id you need, storing that in its own variable for later use. Remember that jsonPath is still a CheckBuilder, so you can't just access the result directly - it might have failed to match.

Converting it to an Option[String] seems like a reasonable thing to do though.

So your final few lines would become:

    ...     .check(       jsonPath("$.id").saveAs("myresponseId")     )   ) ).exec(session => {   val maybeId = session.get("myresponseId").asOption[String]   println(maybeId.getOrElse("COULD NOT FIND ID"))   session }) 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!