7

I've tried different approaches that I've found on Gatling.io, but my problem still persists. There's an API which returns a short response in JSON format when I send a GET request.

GET request:

http://localhost:some_port/api/endpoint1?parameter1=1234&parameter2=5678

Response:

{"transaction":"6d638b9b-f131-41b1-bd07-0d1c6a1d4bcc","reference":"some_text"}

I need to get transaction value from the response and use it in another request.

Next request:

http://localhost:some_port/api/endpoint2?transaction=$transactionValue&parameter=8

So far I've tried using regex, jsonPath with Int or String values but the result is 0 or None.

This is my scenario code so far:

import io.gatling.core.Predef._
import io.gatling.http.Predef._

class class1 extends Simulation {

    val httpProtocol = http
        .baseURL("http://localhost:port")
        .inferHtmlResources()
        .acceptHeader("text/html,application/json")
        .acceptEncodingHeader("gzip, deflate")
        .acceptLanguageHeader("en-US,en;q=0.9,hr;q=0.8,sr;q=0.7,bs;q=0.6")
        .userAgentHeader("Mozilla/5.0 (X11; Fedora; Linux x86_64)")

    val headers = Map(
        "Content-Type" -> "application/json")

    val uri1 = "http://localhost:port/api/endpoint1"
    val uri2 = "http://localhost:port/api/endpoint2"

    val scn = scenario("getEndpoint1")
        .exec(http("endpoint1")
            .get("/api/endpoint1?parameter1=1234&parameter2=5678")
            .headers(headers)
      .check(jsonPath("$.transaction").findAll.saveAs("transaction")))
    .pause(3)
    .exec(session => {
      val transaction = session("transaction").asOption[String]
      session
    }).exec(http("endpoint2").get(uri2 +s"/transaction=${"transaction"}&parameter=8").headers(headers))

    setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)
}

If you have any suggestions or see something I'm doing wrong, it will be greatly appreciated.

Sinisa Brzak
  • 123
  • 1
  • 1
  • 8
  • 1
    Have you tried `.check(jsonPath("$..transaction")).saveAs("transaciton")`? Checking out this [example](https://gatling.io/docs/current/advanced_tutorial/#step-03-use-dynamic-data-with-feeders-and-checks) might help anyway... – Phonolog Jan 18 '18 at 09:23
  • I've tried that option, and you're right that was the problem with the code, but there is another problem. When I try to use the transaction variable out of the session it results in "failed to execute: No attribute named 'transaction' is defined", I've tried also to assign that value to a variable but the result was the same (couldn't use that variable outside of session). I'm currently trying to figure out a way to pass that value outside of the session or finding another approach to this problem. Have any ideas? – Sinisa Brzak Jan 18 '18 at 14:03
  • Hmm might be a typo... You misspelled transaction in the saveAs-method: .saveAs("transa**cit**on"). – Phonolog Jan 18 '18 at 15:55
  • That was a typo when I edited the code in the post it doesn't exist in my actual code. – Sinisa Brzak Jan 18 '18 at 16:16
  • 4
    Ok, I've found a solution. As [Phonolog](https://stackoverflow.com/users/5730444/phonolog) said, jsonPath was not looking at the right parameter, correct was `jsonPath("$..transaction)`. Next problem was that EL variable couldn't be used outside of session. My fix was to forget calling the session at all and to find a different approach. `jsonPath("$..transaction").findAll.saveAs("transaction")` was saving a Vector which created complications, the solution was to use `find` instead of `findAll`. The rest was pretty much straightforward. – Sinisa Brzak Jan 19 '18 at 10:45
  • 1
    @SinisaBrzak please do post the code that worked as an answer (it's perfectly fine to answer your own question on SO). – Paulo Merson Oct 11 '18 at 14:55
  • @PauloMerson you can see the solution in the comment above yours – Sinisa Brzak Oct 24 '18 at 13:20
  • Providing a full answer to this post would have been helpful, as @PauloMerson suggested – fose Dec 14 '21 at 15:17

0 Answers0