Hi all,
I’ve been busting my brain for a while solving the following issue. Hopefully someone can help me.
I am posting a get request to an API which results in the following response body (JSON see below). I want to use this response body and post it to another API (API → JSON → API). The catch is that the data key must be removed from the body, before I can post the actual JSON message.
Example Json (a snippet from the response body from 1st API)
{
"data": [{
“price”: {
“NewPrice”: 1
},
“shipping”: [{
“Newprice”: 2
}]
}]
}
Attempts;
-
I’ve implemented Jsonpath and seems to be a solution but it wraps the JSON as an array [ … ] and therefore can not use it for posting to the second API.
-
I’ve tried transformResponse and use stripPrefix and stripSuffix to remove the data key. But ran into the issue that I the response is a string not a bytearray.
-
I’ve tried to use a regex (can’t get it to work). Any ideas on that?
Code snippet of one of the million attempts
package my.gatling
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
class OfferApiSimulationTest extends Simulation {
val csvFile = “urls.csv” // CSV input file
val secondApiUrl = “2nd.api.url” //
val run = true
val userUrls = csv(csvFile).circular // queue, shuffle, random, circular
val scn = scenario(“Running OfferApiSimulation”).doIf(run == true) // .repeat(10) // repeat the Simulation a number of times
{
during(60 seconds) {
feed(userUrls)
.exec(
http(“Search”)
.get("${url}")
.check(jsonPath("$.data[*]").findAll.saveAs(“body”))
// .transformResponse {
// case response if response.isReceived =>
// println("*** creating ResponseWrapper")
// new ResponseWrapper(response) {
// val replace : String = response.body.string.stripPrefix("{“data”:[").stripSuffix("]}")
//
// override var replace = response.body.string.stripPrefix("{“data”:[").stripSuffix("]}")
// }
//}.check(regex(".*").findAll saveAs(“newBody”))
).exec(
http(“Next API call”)
.post(secondApiUrl)
.header(“Accept”, “application/json”)
.header(“Content-Type”, “application/json”)
.body(StringBody("${body}")).asJSON
//.body(StringBody("${newBody}")).asJSON
)
}
}
setUp(scn.inject(rampUsers(10) over (60 seconds)))
}