OthK
OthK

Reputation: 55

How to save a header from an http response

i'm trying to build a scenario where the user log in first, then do something before logging out. The problem is that i want to save the header response from the log in request to use it in my next request.

When a user log in, he gets an header response containing the Authorization header, with the token.

Here is my code, but it's not working :

val LoggingTest = scenario("Basic Scenario")
      .exec(http("Logging")
      .post("/login")
      .body(
        StringBody("""{"name" : "test",
          "password" : "test"}""")
      )
      .check(header("Authorization").saveAs("token"),status.is(200))
    ).pause(15)

    .exec(http("check")
    .get("/sayhi")
    .header("Authorization",s"${token}")
    .check(status.is(200))
    ).pause(15)

How can i fix it please ?

Upvotes: 2

Views: 882

Answers (2)

Stéphane LANDELLE
Stéphane LANDELLE

Reputation: 6608

It's not s"${token}" but "${token}" without the s.

Sadly, IntelliJ automatically adds this s because it thinks you want to use Scala's String interpolation while you want to use Gatling Expression Language.

Upvotes: 2

Valy Dia
Valy Dia

Reputation: 2851

This is how you can do it:


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

val LoggingTest: ScenarioBuilder = scenario("Basic Scenario")
  .exec(http("Logging")
     .post("/login")
     .body(
        StringBody("""{"name" : "test",
        "password" : "test"}""")
     )
     .check(header("Authorization").saveAs("token"),status.is(200))
   ).pause(15)
   .exec( 
     http("check")
       .get("/sayhi")
       .header("Authorization", session => session("token").validate[String])
       .check(status.is(200))
   ).pause(15)

Upvotes: 3

Related Questions