简体   繁体   中英

Send Authorization Token | Gatling

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)

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")

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM