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

**URL:** <https://community.gatling.io/t/cannot-pass-the-response-value-of-first-api-request-to-the-second-api-request/8031>\
**Category:** Gatling (Open-Source)\
**Created:** [June 15, 2023, 6:00pm UTC](https://community.gatling.io/t/cannot-pass-the-response-value-of-first-api-request-to-the-second-api-request/8031 "2023-06-15T18:00:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julius](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Julius](https://community.gatling.io/u/Julius)\
**Post date:** [June 15, 2023, 6:00pm UTC](https://community.gatling.io/t/cannot-pass-the-response-value-of-first-api-request-to-the-second-api-request/8031/1 "2023-06-15T18:00:26Z")

</div>

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.

```auto
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)

```

}

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [June 15, 2023, 6:37pm UTC](https://community.gatling.io/t/cannot-pass-the-response-value-of-first-api-request-to-the-second-api-request/8031/2 "2023-06-15T18:37:09Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Julius](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Julius](https://community.gatling.io/u/Julius)\
**Post date:** [June 16, 2023, 8:25am UTC](https://community.gatling.io/t/cannot-pass-the-response-value-of-first-api-request-to-the-second-api-request/8031/3 "2023-06-16T08:25:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [June 16, 2023, 1:56pm UTC](https://community.gatling.io/t/cannot-pass-the-response-value-of-first-api-request-to-the-second-api-request/8031/4 "2023-06-16T13:56:25Z")

</div>

`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.

---

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/b/b148e0742de35c6280726cabf3ecd3de2d320f63.png) [@system](https://community.gatling.io/u/system)\
**Post date:** [July 16, 2023, 1:56pm UTC](https://community.gatling.io/t/cannot-pass-the-response-value-of-first-api-request-to-the-second-api-request/8031/5 "2023-07-16T13:56:47Z")

</div>

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