title: Passing Bearer token through variable using Gatling
body: <p>I am learning the Gatling tool and stuck in developing scenarios for Secure Http API calls. I have created a scenario in which I am able to get the bearer token and save it in the variable(Token), but the variable(Token) is not passing its value in the authorization header.</p>
  <p>Here's my code pleaser review it,</p>
  <p>The value of the token variable is not getting by the following code line,
  .authorizationHeader(s&quot;Bearer $token&quot;)</p>
  <p>======================================================</p>
  <pre class="lang-scala prettyprint-override"><code>
  import io.gatling.core.Predef._
  
  import io.gatling.http.Predef._
  
  import scala.concurrent.duration._
  
  import scala.collection.JavaConversions._
  
  
  class SampleToken2 extends Simulation {
  
  
  
  
    //Token define
    
    private var token: String = &quot;&quot;
    
    val auth = scenario(&quot;Retrieve Token&quot;)
      .exec(
        http(&quot;POST Auth Req&quot;)
          .post(&quot;http://iserver:9092/login&quot;)
          .body(ElFileBody(&quot;bodies/inventalogin.json&quot;)).asJson
          .check(bodyString.saveAs(&quot;Auth_Response&quot;))
          .check(status.is(200))
          .check(jsonPath(&quot;$.token&quot;).find.saveAs(&quot;accesskey&quot;)))
      .exec{session =&gt; { token = session(&quot;accesskey&quot;).as[String]
        session}}
    
  
    
  
    //Header Define  
    
    val httpConf = http
      .baseUrl(&quot;http://iaserver:9092&quot;)
      .authorizationHeader(s&quot;Bearer $token&quot;)
      .acceptHeader(&quot;application/json, */*&quot;)
      .acceptCharsetHeader(&quot;UTF-8&quot;) // Here are the common headers
      .doNotTrackHeader(&quot;1&quot;)
      .acceptLanguageHeader(&quot;en-UK,en;q=0.5&quot;)
      .acceptEncodingHeader(&quot;gzip, deflate&quot;)
      .userAgentHeader(&quot;Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0&quot;)
      .shareConnections
      .proxy(Proxy(&quot;localhost&quot;, 8888).httpsPort(8888))
  
  
      def  myTestObjectMethod = {
        exec { session =&gt; println(&quot;token print2&quot;); session }
        exec { session =&gt; println(token:String); session }
        exec(http(&quot;Get all devices with pagination&quot;)
          .get(&quot;/devices/getAllDevices?page=0&amp;size=200&quot;)
          .check(status.in(200 to 210)))
          .pause(1, 20)
      }
  
  
  
    val scn = scenario(&quot;my_actual_load_test&quot;).exec(myTestObjectMethod)
  
  
  
    setUp(
      auth.inject(constantUsersPerSec(1) during (1 seconds)),
      scn.inject(nothingFor(2 seconds),
        constantUsersPerSec(50) during (300 seconds)
      )
      .protocols(httpConf))
      .assertions(global.responseTime.max.lt(500)) 
      .assertions(forAll.failedRequests.percent.lte(1)) 
      .assertions(global.responseTime.mean.lte(100))
  
  }
  
  </code></pre>