# Gatling 1.5.x: Add HMAC authorization header with EvaluatableString

**URL:** <https://community.gatling.io/t/gatling-1-5-x-add-hmac-authorization-header-with-evaluatablestring/720>\
**Category:** Gatling (Open-Source)\
**Created:** [December 11, 2013, 5:23pm UTC](https://community.gatling.io/t/gatling-1-5-x-add-hmac-authorization-header-with-evaluatablestring/720 "2013-12-11T17:23:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lars\_Mulanski](https://avatars.discourse-cdn.com/v4/letter/l/ed8c4c/32.png) [@Lars\_Mulanski](https://community.gatling.io/u/Lars_Mulanski)\
**Post date:** [December 11, 2013, 5:23pm UTC](https://community.gatling.io/t/gatling-1-5-x-add-hmac-authorization-header-with-evaluatablestring/720/1 "2013-12-11T17:23:36Z")

</div>

Hi all,

What would be a better (more functional) way to add an authorization header to an request which URL is based on a dynamic string?

Thanks for your help in advance,  
Lars

Here is how the code currently looks like …

package scenarios

import com.excilys.ebi.gatling.core.Predef.\_  
import com.excilys.ebi.gatling.http.Predef.\_

import akka.util.duration.\_

import bootstrap.\_  
import basic.{Configuration, Auth}  
import net.jalg.hawkj.{Algorithm, HawkContext}

object Navigation {

val productIds = csv(“product\_ids.csv”).random

def getAuthorizationHeader(verb: String, path: String): String = {  
val ctxt = HawkContext  
.request(verb, path, Configuration.HOST, 443) // Assume HTTPS all over the place  
.credentials(Configuration.CLIENT\_ID, Configuration.CLIENT\_KEY, Algorithm.SHA\_256)  
.tsAndNonce(System.currentTimeMillis() / 1000, “e0bee6ad552d”)  
.build()  
ctxt.createAuthorizationHeader().toString  
}

val scn = scenario(“Scenario Navigation”)  
.repeat(2) {  
feed(productIds)  
.exec(session =\>  
session  
// TODO Inject ??  
.setAttribute(“product\_url”, “/api/products/” + session.getTypedAttribute(“product\_id”))  
.setAttribute(“auth\_header”, getAuthorizationHeader(“GET”, “/api/products/” + session.getTypedAttribute(“product\_id”)))  
)  
.exec(  
http(“request\_product”)  
.get("${product\_url}")  
.header(“Authorization”, “${auth\_header}”)  
.header(“Accept”, “application/vnd.example.product+xml”)  
.check(status.is(200)))  
.pause(5 milliseconds, 50 milliseconds)  
}

}

PS: I found a post which seem to address a similar problem: [https://groups.google.com/forum/#!searchin/gatling/hmac$20header/gatling/Xnmn1NepiTQ/VzRBWvqdZ2AJ](https://groups.google.com/forum/#!searchin/gatling/hmac%2420header/gatling/Xnmn1NepiTQ/VzRBWvqdZ2AJ)  
but am not able to see how this could work out for Gatling 1.

---

<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:** [December 11, 2013, 5:37pm UTC](https://community.gatling.io/t/gatling-1-5-x-add-hmac-authorization-header-with-evaluatablestring/720/2 "2013-12-11T17:37:46Z")

</div>

Session.getAttributeAsOption handles missing attribute, but not type errors, but that’s the best you can have with Gatling 1.

.exec { session =\>  
val updatedSession =  
for {  
productId ← session.getAttributeAsOptionString  
authorizationHeader = getAuthorizationHeader(“GET”, “/api/products/” + productId)  
} yield session  
.setAttribute(“product\_url”, productId)  
.setAttribute(“auth\_header”, authorizationHeader)

updatedSession.getOrElse(session)  
}

---

<div class="post-metadata">

**Author:** ![Lars\_Mulanski](https://avatars.discourse-cdn.com/v4/letter/l/ed8c4c/32.png) [@Lars\_Mulanski](https://community.gatling.io/u/Lars_Mulanski)\
**Post date:** [December 12, 2013, 7:08am UTC](https://community.gatling.io/t/gatling-1-5-x-add-hmac-authorization-header-with-evaluatablestring/720/3 "2013-12-12T07:08:17Z")

</div>

Thanks for your answer, Stéphane.

Actually I was interested to know if there is a way to get rid of the setAttribute block by evaluating the product\_id attribute inside the getAuthorizationHeader method and by which means? How to resolve with the help of ELParser?

http(“request\_product”)  
.get("/api/products/${product\_id}") // ← works fine

.header(“Authorization”, getAuthorizationHeader(“GET”, “/api/products/${product\_id}”)) // ← how to evaluate product\_id ?

Thanks,  
Lars

---

<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:** [December 12, 2013, 8:48am UTC](https://community.gatling.io/t/gatling-1-5-x-add-hmac-authorization-header-with-evaluatablestring/720/4 "2013-12-12T08:48:34Z")

</div>

The thing is Gatling 1 Session API can’t handle missing attributes and non matching types properly, so you have to be sure that the attribute exists and it has the correct type (which is sure if it’s injected by a feeder, but not if it’s captured on a response because the request might have crashed).  
If not, you’ll get an Exception. Gatling should handle it properly, but creating Exceptions is an expensive process, hence the Gatling 2 API based on Validation.

so:  
.header(“Authorization”, session =\> getAuthorizationHeader(“GET”, “/api/products/” + session.getTypedAttributeString))
