Cannot pass the response value of first api request to the second api request

I have a gatling code below where I want the value of the listingid in the response of the first api be passed to the request of the second api. However, only the first api is successful. Kindly help.

class ClassifiedGenerator extends Simulation{

  // protocol
  val httpProtocol: HttpProtocolBuilder = http
    .baseUrl("https://url.com")

  // scenario
  //first api
  val createlistingidscn: ScenarioBuilder = scenario("create listingid")
    .exec(
      http("create listingid")
        .post("/api/Listing")
        .header("accept", "application/json")
        .body(RawFileBody("src/test/resources/testCreateClassified.json")).asJson
        .check(
          status is 201,
          jsonPath("$.listingId").saveAs("listingId")
        )
    )
 
  //second api request
  val publishlistingidscn: ScenarioBuilder = scenario("publish listingid")
    .exec(
      http("publish listingid")
        .put("/api/Listing/PublishByAgencyId")
        .header("accept", "application/json")
        .body(StringBody(s"""{
                           |  "agencyId": 240565, //hardcode
                           |  "publicationTypeId": 2, //hardcode
                           |  "listingIds": [
                           |    ${listingId} //variable value of listingid from the response of the first api
                           |  ]
                           |}""".stripMargin)).asJson
    )



  //setUp
  setUp(
    createlistingidscn.inject(atOnceUsers(1)),
    publishlistingidscn.inject(atOnceUsers(1))
  ).protocols(httpProtocol)

}

Hi @Julius,

First, a scenario is the description of the script for one virtual user.
The session is dedicated to one of such virtual user.
As you coded 2 different scenario, they cannot share the value through the session.

Second, please use the uptodate syntax #{listingId}. The former syntax with a dollar ($) is legacy because of what you did: ask scala to interpret the string, instead of letting Gatling to use its Expression language (mind the s at the beginning of the string)

Third, you should not use file path from your root folder, but from your classpath.
For your RawFileBody: RawFileBody("/testCreateClassified.json"). Because once compiled into a jar, your file won’t be there. (src/test/resources is usually the root directory of resources that are not compiled)

Hope it helps!
Cheers!

1 Like

Isolating the two scenarios and running it individually is a better way ? I will add a code on the first scenario, something like one that can save the listing ids in an array or list or in file which I can use as a feeder for the second scenario when I run it in a separate scala…If theres other way to do this, I am open to any idea.

saveAs stored the data in the memory space of the virtual user that executed the request. So there’s no way this data is visible from a different visual user, all the more from a different scenario.

For this need, you should only have 1 single scenario that would perform your 2 requests.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.