4
votes

I am performance testing our Enterprise API using Gatling. While testing with "Postman" I used to pass following Headers:

  • API Key
  • Authorization Bearer Token

It would be great if someone could help me: How can I pass "API-KEY" and "Authorization Bearer" token as a header in a Gatling request?

Please see my code below:

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

    val httpConf = http
        .baseURL(perfProdURL)
        .acceptHeader("application/json, */*")
        .acceptCharsetHeader("UTF-8")

    val scn = scenario("Vertex API Test01")
        .exec(
            http("request_1")
            .post("/details/orders")
            .headers(headers_10)
            .check(status.is(200))
            .body(RawFileBody("/Users/z063011/Sunil/test.json")).asJSON)
2
How about adding it to your already defined headers map: val headers_10 = Map("Content-Type" -> """application/json""", "API-KEY" -> "your_api_key", "Authorization Bearer" -> "auth_bearer")?Phonolog
Yep did that... Working goodSunil
Awesome! I added an answer, feel free to vote up and accept :)Phonolog

2 Answers

3
votes

As mentioned in the comments, you can add additional fields to your already defined headers map like this:

val headers_10 = Map("Content-Type" -> """application/json""", "API-KEY" -> "your_api_key", "Authorization Bearer" -> "auth_bearer")

You might also want to check out the docs on headers.

3
votes

I realize this is older and answered, but as an alternative you can also add your Bearer token as follows:

val token: String = getTokenFunction()

val httpConf = http
  .baseUrl(myUrlForLoadTests)
  .authorizationHeader(s"Bearer $token")
  .acceptHeader("application/json, */*")
  .acceptCharsetHeader("UTF-8")