# Getting a token and how to build the .exec

**URL:** <https://community.gatling.io/t/getting-a-token-and-how-to-build-the-exec/1869>\
**Category:** Gatling (Open-Source)\
**Created:** [January 8, 2015, 9:45am UTC](https://community.gatling.io/t/getting-a-token-and-how-to-build-the-exec/1869 "2015-01-08T09:45:48Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![fugmag](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@fugmag](https://community.gatling.io/u/fugmag)\
**Post date:** [January 8, 2015, 9:45am UTC](https://community.gatling.io/t/getting-a-token-and-how-to-build-the-exec/1869/1 "2015-01-08T09:45:48Z")

</div>

I want to do a call to a token-service like this in Gatling:

`

---- REQUEST: ----

POST [http://idpt.company.com/ids/connect/token](http://idpt.coop.no/ids/connect/token) HTTP/1.1

Authorization: Basic bW9iaWxlX2NsaWVudDptb2JpbGVfc2VjcmV0

host: [idpt.company.](http://idpt.coop.no/)com

content-type: application/x-www-form-urlencoded; charset=utf-8

content-length: 103

Connection: keep-alive

username=theusername&password=thepassword&scope=small&proxy=http%3A%2F%2Flocalhost%3A8888&grant\_type=password

`

This (POST) request should respons with a token.

My question is how to I “translate” the request above into an .exec in Gatling?

The Authorization: Basic i.e. how can I state that in the .exec?  
It is static and can be the same for every request I make.

Cheers

---

<div class="post-metadata">

**Author:** ![fugmag](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@fugmag](https://community.gatling.io/u/fugmag)\
**Post date:** [January 8, 2015, 10:33am UTC](https://community.gatling.io/t/getting-a-token-and-how-to-build-the-exec/1869/2 "2015-01-08T10:33:01Z")

</div>

I solved this like this:

`

```auto
.exec(
  http("getToken")
    .post("https://id.company.com/ids/connect/token")
    .headers(Map("Content-Type" -> "application/x-www-form-urlencoded; charset=utf-8" , "Authorization" -> "Basic bW9iaWxlX2xxxxxxmV0"))
    .body(StringBody("username=theusername&password=thepassword&scope=small&proxy=http%3X%2F%2Flocalhost%3A8888&grant_type=password"))
    .asFormUrlEncoded
)

```

`

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [January 8, 2015, 3:11pm UTC](https://community.gatling.io/t/getting-a-token-and-how-to-build-the-exec/1869/3 "2015-01-08T15:11:56Z")

</div>

Actually, you shouldn’t craft the Authorization header yourself (nor hardcode it).  
This is plain old Basic auth: [http://gatling.io/docs/2.1.2/http/http\_protocol.html#authentication](http://gatling.io/docs/2.1.2/http/http_protocol.html#authentication)

---

<div class="post-metadata">

**Author:** ![fugmag](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@fugmag](https://community.gatling.io/u/fugmag)\
**Post date:** [January 8, 2015, 3:45pm UTC](https://community.gatling.io/t/getting-a-token-and-how-to-build-the-exec/1869/4 "2015-01-08T15:45:45Z")

</div>

Ok.  
I know how my Authorization header is made.  
It is just a base64encode of two values, lets say:  
c29tZV9jbGllbnQ6c29tZV9zZWNyZXQ=

which decodes to:  
some\_client:some\_secret

Is there a way to decode ‘some\_client’ and ‘some\_secret’ on the fly?

Should I do it in the header-mapping

---

<div class="post-metadata">

**Author:** ![Steven\_Zaluk](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@Steven\_Zaluk](https://community.gatling.io/u/Steven_Zaluk)\
**Post date:** [January 8, 2015, 8:08pm UTC](https://community.gatling.io/t/getting-a-token-and-how-to-build-the-exec/1869/5 "2015-01-08T20:08:43Z")

</div>

I had to do something similar where I had to dynamically create an Authorization and Content-MD5 header for each request that is computed based on the payload of the request. I created a SignatureCalculator and that works perfectly.

–Steve

---

<div class="post-metadata">

**Author:** ![fugmag](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@fugmag](https://community.gatling.io/u/fugmag)\
**Post date:** [January 8, 2015, 8:32pm UTC](https://community.gatling.io/t/getting-a-token-and-how-to-build-the-exec/1869/6 "2015-01-08T20:32:03Z")

</div>

Would it be possible to share the "calculator"?

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [January 8, 2015, 8:39pm UTC](https://community.gatling.io/t/getting-a-token-and-how-to-build-the-exec/1869/7 "2015-01-08T20:39:13Z")

</div>

Here, we’re talking about plain old [Basic authentication](http://en.wikipedia.org/wiki/Basic_access_authentication).  
As it’s a very very very very standard mechanism, of course Gatling supports it out of the box.  
I already sent a pointer to the documentation in a previous mail in this very same thread!

---

<div class="post-metadata">

**Author:** ![fugmag](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@fugmag](https://community.gatling.io/u/fugmag)\
**Post date:** [January 9, 2015, 9:56pm UTC](https://community.gatling.io/t/getting-a-token-and-how-to-build-the-exec/1869/8 "2015-01-09T21:56:07Z")

</div>

Ok,

So then I have:

`

```auto
package com.site

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

class idpSimulation extends Simulation{

  val httpConf = http
    .baseURL("https://id.site.com/ids")
    .acceptEncodingHeader("gzip,deflate")
    .headers(Map("Content-Type" -> "application/x-www-form-urlencoded; charset=utf-8"))
    .authorizationHeader("Basic bW9PaWxlX2NsaWZudDptbpJpxVfc2VjcmV0")
    .disableUrlEscaping

  val scn = scenario("Scenario")

    .feed(csv("data/testdata.csv").random)

    .exec(
      http("getToken")
        .post("/connect/token").basicAuth("${MemberId}", "password")
        //.body(StringBody("username=${MemberId}&password=password&scope=legacy&proxy=http%3A%2F%2Flocalhost%3A8888&grant_type=password"))
        .asFormUrlEncoded
        .check(jsonPath("$.access_token").exists.saveAs("token")))

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

```

`

But what do I do with the remaing part of the string body (commented out above). How do I define the part:

`

```auto
*...*scope=legacy&proxy=http%3A%2F%2Flocalhost%3A8888&grant_type=password

```

`

in my simulation? still as a string body without the username and password?

---

<div class="post-metadata">

**Author:** ![fugmag](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@fugmag](https://community.gatling.io/u/fugmag)\
**Post date:** [January 9, 2015, 10:03pm UTC](https://community.gatling.io/t/getting-a-token-and-how-to-build-the-exec/1869/9 "2015-01-09T22:03:58Z")

</div>

If I remove the username and password from the string body and put it into a basicauth and run the simulation my logging says:

`

---

<div class="post-metadata">

**Author:** ![Abhinav\_Gogna](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@Abhinav\_Gogna](https://community.gatling.io/u/Abhinav_Gogna)\
**Post date:** [January 9, 2015, 10:43pm UTC](https://community.gatling.io/t/getting-a-token-and-how-to-build-the-exec/1869/10 "2015-01-09T22:43:55Z")

</div>

If you look at the logs, it shows that you have two authorization headers in your http request. You only need to send one.

one way would be

` `

---

<div class="post-metadata">

**Author:** ![fugmag](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@fugmag](https://community.gatling.io/u/fugmag)\
**Post date:** [January 9, 2015, 11:16pm UTC](https://community.gatling.io/t/getting-a-token-and-how-to-build-the-exec/1869/11 "2015-01-09T23:16:57Z")

</div>

Yes but what do I do with the remaining part of the:

`

```auto
.body(StringBody("username=${MemberId}&password=password&scope=legacy&proxy=http%3A%2F%2Flocalhost%3A8888&grant_type=password")

```

`

as in:

`

```auto
scope=legacy&proxy=http%3A%2F%2Flocalhost%3A8888&grant_type=password"

```

`

I cannot just say:

`

```auto
.body(StringBody("scope=legacy&proxy=http%3A%2F%2Flocalhost%3A8888&grant_type=password"))

```

`

That is what causing the error reported earlier.

---

<div class="post-metadata">

**Author:** ![Abhinav\_Gogna](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@Abhinav\_Gogna](https://community.gatling.io/u/Abhinav_Gogna)\
**Post date:** [January 10, 2015, 12:15am UTC](https://community.gatling.io/t/getting-a-token-and-how-to-build-the-exec/1869/12 "2015-01-10T00:15:53Z")

</div>

I am not sure what exactly that is. But you can try following options

1. Leave that as is. Since this is not part of the header, it may not give 400 error
2. It could be a formParam - try using .formParam [http://gatling.io/docs/2.1.2/http/http\_request.html#query-parameters](http://gatling.io/docs/2.1.2/http/http_request.html#query-parameters)
3. If it is a proxy then declare proxy setting globally - [http://gatling.io/docs/2.1.2/http/http\_protocol.html#proxy-parameters](http://gatling.io/docs/2.1.2/http/http_protocol.html#proxy-parameters)

Debug it.

---

<div class="post-metadata">

**Author:** ![Meena\_Dappili](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@Meena\_Dappili](https://community.gatling.io/u/Meena_Dappili)\
**Post date:** [July 23, 2019, 6:51am UTC](https://community.gatling.io/t/getting-a-token-and-how-to-build-the-exec/1869/13 "2019-07-23T06:51:34Z")

</div>

I am getting the same token for login response like [Authorization : Bearer fsdjflkajsfjlasjfjdsafjajfkafkdsajkfalj–\> token value]i could able to capture the token.

this token is same for all further requests, i am unable to pass this header value to all the further requests.  
the requests are like api call related http requests.

Can you help me with how to add this header to further request.

i tried with

val sessionHeaders = Map(“Authorization” → “Bearer ${token}”,  
“Content-Type” → “application/json”)

i passed the same header value like below.

.get("/smee/common/categoryList") -----\> this http request will trigger api call.  
.headers(sessionHeaders)

please any one help me on this. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![G\_Madhana\_Mohana\_Red](https://avatars.discourse-cdn.com/v4/letter/g/b38774/32.png) [@G\_Madhana\_Mohana\_Red](https://community.gatling.io/u/G_Madhana_Mohana_Red)\
**Post date:** [July 27, 2019, 5:51am UTC](https://community.gatling.io/t/getting-a-token-and-how-to-build-the-exec/1869/14 "2019-07-27T05:51:05Z")

</div>



---

<div class="post-metadata">

**Author:** ![G\_Madhana\_Mohana\_Red](https://avatars.discourse-cdn.com/v4/letter/g/b38774/32.png) [@G\_Madhana\_Mohana\_Red](https://community.gatling.io/u/G_Madhana_Mohana_Red)\
**Post date:** [July 27, 2019, 5:56am UTC](https://community.gatling.io/t/getting-a-token-and-how-to-build-the-exec/1869/15 "2019-07-27T05:56:56Z")

</div>

Hi Meena,

Please follow the below sample template/code for header implementation.

object xxxxx {  
val xxxxxx =exec(http(“Main\_Request”)  
.post("/")  
.headers(headers\_2)  
.header(“Authorization”,“Bearer ${Beaererid}”)  
.body(StringBody("""\<\<\<\>\>\>"""))  
.check(status.in(200))  
)  
}

or

val headers\_2 = Map(  
“Accept” → “_/_”,  
“Accept-Encoding” → “gzip, deflate, br”,  
“Connection” → “keep-alive”,  
“content-type” → “application/json”,  
“region” → “IN”,  
“x-status” → “Auth”,  
“X-APIKEY” → “\<your api key”  
)

---

<div class="post-metadata">

**Author:** ![Meena\_Dappili](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@Meena\_Dappili](https://community.gatling.io/u/Meena_Dappili)\
**Post date:** [July 29, 2019, 8:49am UTC](https://community.gatling.io/t/getting-a-token-and-how-to-build-the-exec/1869/16 "2019-07-29T08:49:51Z")

</div>

Hi Madan,

import scala.concurrent.duration.\_

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

class ChangeRecordedSimulation extends Simulation {

val sessionHeaders = Map(“Authorization” → “Bearer ${authToken1}”,  
“Content-Type” → “application/json”)

val httpProtocol = http  
.baseUrl(“[https://xxxx.xxxxx.com](https://xxxx.xxxxx.com)”)  
.inferHtmlResources()  
.acceptHeader(“application/json, text/plain, _/_”)  
.userAgentHeader(“Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36”)

val headers\_0 = Map(  
“Accept” → “text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,_/_;q=0.8,application/signed-exchange;v=b3”,  
“Upgrade-Insecure-Requests” → “1”)

val headers\_17 = Map(  
“Content-Type” → “application/json;charset=UTF-8”,  
“Origin” → “[https://xxx.xxx.com](https://xxx.xxx.com)”,  
“X-Requested-With” → “XMLHttpRequest”)

val headers\_18 = Map(  
“X-Requested-With” → “XMLHttpRequest”,  
“authorization” → “Bearer ${authToken1}”,  
“content-type” → “application/json”)

val headers\_20 = Map(  
“X-Requested-With” → “XMLHttpRequest”,  
“authorization” → “Bearer ${authToken1}”)

val headers\_21 = Map(  
“Content-Type” → “application/json;charset=UTF-8”,  
“Origin” → “[https://xxxx.xxxx.com](https://xxxx.xxxx.com)”,  
“X-Requested-With” → “XMLHttpRequest”,  
“authorization” → “Bearer ${authToken1}”)

val headers\_37 = Map(  
“Origin” → “[https://xxxx.xxxx.com](https://xxxx.xxxx.com)”,  
“X-Requested-With” → “XMLHttpRequest”,  
“authorization” → “Bearer ${authToken1}”,  
“content-type” → “application/json”)

val uri2 = “[https://fonts.googleapis.com:443/css](https://fonts.googleapis.com:443/css)”  
val uri3 = “[https://cdnjs.cloudflare.com:443/ajax/libs](https://cdnjs.cloudflare.com:443/ajax/libs)”

val scn = scenario(“ChangeRecordedSimulation”)  
.exec(http(“request\_0”)  
.get("/xxx/ui/")  
.headers(headers\_0))  
.pause(1)  
.exec(http(“request\_1”)  
.get(uri3 + “/ionicons/2.0.1/css/ionicons.min.css”)  
.headers(headers\_0)  
.resources(http(“request\_2”)  
.get(uri3 + “/font-awesome/4.5.0/css/font-awesome.min.css”)  
.headers(headers\_0),  
http(“request\_3”)  
.get(uri2 + “?family=Roboto+Condensed”)  
.headers(headers\_0),  
http(“request\_4”)  
.get("/xxx/ui/static/css/\_all-skins.min.css")  
.headers(headers\_0),  
http(“request\_5”)  
.get("/xxxx/ui/static/css/AdminLTE.min.css")  
.headers(headers\_0),  
http(“request\_6”)  
.get("/xxxx/ui/static/css/bootstrap.min.css")  
.headers(headers\_0),  
http(“request\_7”)  
.get("/xxxx/ui/static/css/pace.min.css")  
.headers(headers\_0),  
http(“request\_8”)  
.get("/xxxx/ui/static/js/jquery.slimscroll.min.js")  
.headers(headers\_0),  
http(“request\_9”)  
.get("/xxxx/ui/static/js/manifest.31b3b1b0528463613ec1.js")  
.headers(headers\_0),  
http(“request\_10”)  
.get(uri2 + “?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic”)  
.headers(headers\_0),  
http(“request\_11”)  
.get("/xxxx/ui/static/js/jquery-2.2.3.min.js")  
.headers(headers\_0),  
http(“request\_12”)  
.get("/xxxx/ui/static/js/bootstrap.min.js")  
.headers(headers\_0),  
http(“request\_13”)  
.get("/xxxx/ui/static/css/app.176431924930ce0b6fc1f73d1f4795c0.css")  
.headers(headers\_0),  
http(“request\_14”)  
.get("/xxxx/ui/static/js/pace.min.js")  
.headers(headers\_0),  
http(“request\_15”)  
.get("/xxxx/ui/static/js/app.99a07150331050539321.js")  
.headers(headers\_0),  
http(“request\_16”)  
.get("/xxxx/ui/static/js/vendor.c94789110b7f7b9fc3be.js")  
.headers(headers\_0),  
http(“LogIn”)  
.post("/xxxx/usermanagement/userCurrent")

**.check(jsonPath("$…token").exists.saveAs(“authToken1”))**

.headers(headers\_17)  
.body(RawFileBody(“ChangeRecordedSimulation\_0017\_request.txt”)),  
http(“request\_18”)  
.get("/xxxx/common/clientUserInactiveTimeout")

.headers(headers\_18),  
http(“request\_19”)  
.get("/xxxx/common/help-menu2?userFunctionId=10")  
.headers(sessionHeaders),  
//.headers(headers\_18),  
http(“request\_20”)  
.get("/xxxx/notification/dashboardStatistics?userFunctionGroupId=10&categoryCodeList=")  
.headers(sessionHeaders),  
//.headers(headers\_20),  
http(“request\_21”)  
.post("/xxxx/changemanagement/api/checkUserHierarchyMapping")  
.headers(sessionHeaders)  
//.headers(headers\_21)  
.body(RawFileBody(“ChangeRecordedSimulation\_0021\_request.txt”)),  
http(“request\_22”)  
.get("/xxxx/common/dataTablePreferences?dataTableCode=Step2-From-Datatable1&userSid=17")  
.headers(sessionHeaders),  
//.headers(headers\_20),  
http(“request\_23”)  
.get("/xxxx/common/dataTablePreferences?dataTableCode=Step2-To-Datatable1&userSid=17")  
.headers(headers\_20),  
http(“request\_24”)  
.get("/xxxx/common/dataTablePreferences?dataTableCode=Step2-From-Datatable5&userSid=17")  
.headers(headers\_20),  
http(“request\_25”)  
.get("/xxxx/common/dataTablePreferences?dataTableCode=Step2-To-Datatable5&userSid=17")  
.headers(headers\_20),  
http(“request\_26”)  
.get("/xxxx/common/dataTablePreferences?dataTableCode=Step3-Datatable5&userSid=17")  
.headers(headers\_20),  
http(“request\_27”)  
.get("/xxxx/common/dataTablePreferences?dataTableCode=Step3-Datatable1&userSid=17")  
.headers(headers\_20),  
http(“request\_28”)  
.get("/xxxx/common/dataTablePreferences?dataTableCode=Step4-Datatable1&userSid=17")  
.headers(headers\_20),  
http(“request\_29”)  
.get("/xxxx/common/dataTablePreferences?dataTableCode=Step3-Datatable4&userSid=17")  
.headers(headers\_20),  
http(“request\_30”)  
.get("/xxxx/common/dataTablePreferences?dataTableCode=Step2-From-Datatable2&userSid=17")  
.headers(headers\_20),  
http(“request\_31”)  
.get("/xxxx/common/dataTablePreferences?dataTableCode=Step2-To-Datatable2&userSid=17")  
.headers(headers\_20),  
http(“request\_32”)  
.get("/xxxx/common/dataTablePreferences?dataTableCode=Step3-Datatable2&userSid=17")  
.headers(headers\_20),  
http(“request\_33”)  
.get("/xxxx/common/dataTablePreferences?dataTableCode=Datatable-PCL-CreateByExcelNewChase&userSid=17")  
.headers(headers\_20),  
http(“request\_34”)  
.get("/xxxx/common/searchPanelUserPreferences?userSid=17&searchPanelCode=chaseSearchPanel")  
.headers(headers\_18),  
http(“request\_35”)  
.get("/xxxx/common/searchPanelUserPreferences?userSid=17&searchPanelCode=chaseTagSearchPanel")  
.headers(headers\_18),  
http(“Search”)  
.post("/xxxx/changemanagement/api/newChaseCreateSearch")  
.headers(headers\_21)  
.body(RawFileBody(“ChangeRecordedSimulation\_0036\_request.txt”)),  
http(“request\_37”)  
.put("/xxxx/common/searchPanelUserPreferences")  
.headers(headers\_37)  
.body(RawFileBody(“ChangeRecordedSimulation\_0037\_request.txt”)),  
http(“request\_38”)  
.post("/xxxx/changemanagement/api/newChaseCreateSearchStep3")  
.headers(headers\_21)  
.body(RawFileBody(“ChangeRecordedSimulation\_0038\_request.txt”)),  
http(“request\_39”)  
.post("/xxxx/changemanagement/api/newChaseRequestSave")  
.headers(headers\_21)  
.body(RawFileBody(“ChangeRecordedSimulation\_0039\_request.txt”)))  
//  
)  
.exec { session =\>  
println()  
println(“authToken1”)  
println()  
println( session(“authToken1”).as[String] )  
session  
}

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

**Note:**  
Under UserCurrent request using JasonPath i could able to capture the token, which is generating as a response for login page.  
when i am replacing the token in headers : authorization : bearer {authtoken1}

**getting an error like :**

request\_18: Failed to build request: No attribute named ‘authToken1’ is defined.

**what i want:**  
i want to pass the token to all the further request by replacing in all the **authorization** header value at the top, but it is not working for me.

i even tried : [https://gist.github.com/alanphil/52d03338ba85c9ee0bd894b73ee3f999](https://gist.github.com/alanphil/52d03338ba85c9ee0bd894b73ee3f999) ----\> still failed.

please help me on this, i am really blocked.

Thanks in advance.
