# Wanting to extract some request variables, hash, insert into header

**URL:** <https://community.gatling.io/t/wanting-to-extract-some-request-variables-hash-insert-into-header/553>\
**Category:** Gatling (Open-Source)\
**Created:** [August 28, 2013, 9:30am UTC](https://community.gatling.io/t/wanting-to-extract-some-request-variables-hash-insert-into-header/553 "2013-08-28T09:30:38Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dino\_Fancellu](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@Dino\_Fancellu](https://community.gatling.io/u/Dino_Fancellu)\
**Post date:** [August 28, 2013, 9:30am UTC](https://community.gatling.io/t/wanting-to-extract-some-request-variables-hash-insert-into-header/553/1 "2013-08-28T09:30:38Z")

</div>

Using Gatling 2.0.0-M3a

I have to set a header value to an encryption hash based on the uri, method, datetime, any body.

I can get this all to work if I do it all manually, i.e. call the hashing function with the values I am going to use.

However if there a way from my Get/PostHttpRequestBuilder to do a header(“hash”,somefunc), with somefunc  
getting the above uri, method, body and passing it to the hashing function?

i.e. I don’t want to have to have to repeat myself, but easily set the hash value based on what is already in the request

Thanks.

---

<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:** [August 28, 2013, 9:54am UTC](https://community.gatling.io/t/wanting-to-extract-some-request-variables-hash-insert-into-header/553/2 "2013-08-28T09:54:22Z")

</div>

Nope

---

<div class="post-metadata">

**Author:** ![Dino\_Fancellu](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@Dino\_Fancellu](https://community.gatling.io/u/Dino_Fancellu)\
**Post date:** [August 28, 2013, 10:08am UTC](https://community.gatling.io/t/wanting-to-extract-some-request-variables-hash-insert-into-header/553/3 "2013-08-28T10:08:26Z")

</div>

I suspected as much.

What if AbstractHttpRequestBuilder had a getAttributes:HttpAttributes method?

Its a case class, immutable. Just would be useful to do some post-processing on a builder as described above.

Thanks.

Dno.

---

<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:** [August 28, 2013, 12:32pm UTC](https://community.gatling.io/t/wanting-to-extract-some-request-variables-hash-insert-into-header/553/4 "2013-08-28T12:32:07Z")

</div>

What for? AbstractHttpRequestBuilder.httpAttributes is public.

---

<div class="post-metadata">

**Author:** ![Dino\_Fancellu1](https://avatars.discourse-cdn.com/v4/letter/d/7cd45c/32.png) [@Dino\_Fancellu1](https://community.gatling.io/u/Dino_Fancellu1)\
**Post date:** [August 28, 2013, 12:40pm UTC](https://community.gatling.io/t/wanting-to-extract-some-request-variables-hash-insert-into-header/553/5 "2013-08-28T12:40:37Z")

</div>

Really?

val getOrderDetails=http(“GetOrderDetails”)  
.get(url)

println (getOrderDetails.httpAttributes)

won’t compile.

---

<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:** [August 28, 2013, 12:52pm UTC](https://community.gatling.io/t/wanting-to-extract-some-request-variables-hash-insert-into-header/553/6 "2013-08-28T12:52:41Z")

</div>

Ah, you’re right.

Anyway, you can manually create a RequestBuilder, the constructors are public.

---

<div class="post-metadata">

**Author:** ![Dino\_Fancellu](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@Dino\_Fancellu](https://community.gatling.io/u/Dino_Fancellu)\
**Post date:** [August 29, 2013, 9:24am UTC](https://community.gatling.io/t/wanting-to-extract-some-request-variables-hash-insert-into-header/553/7 "2013-08-29T09:24:22Z")

</div>

I’m not sure how that helps me.

I build up a request, the url uses an orderid from a feed, so will be different with each request, and I have to send a hash header value based on that dynamic url, else the receiving url won’t accept my request.

Dino.

---

<div class="post-metadata">

**Author:** ![Dino\_Fancellu](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@Dino\_Fancellu](https://community.gatling.io/u/Dino_Fancellu)\
**Post date:** [August 29, 2013, 9:34am UTC](https://community.gatling.io/t/wanting-to-extract-some-request-variables-hash-insert-into-header/553/8 "2013-08-29T09:34:48Z")

</div>

Linked to this, might be a solution, just wondering.

I feed in some values thus:

val orderIds = ((1 to 1000) map (x=\>Map(“oid”-\>x)) toArray).circular

…  
.feed(orderIds)

So I have a 1000 repeating “oid” values.

Now, its easy to simply insert that value into a header like this:

.header(“oid”,"${oid}")

However, is there a good way I could call some custom function based on that ${oid} ?

e.g.

.header(“oid”,“myfunc(${oid})”)

Thanks.

---

<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:** [August 29, 2013, 9:47am UTC](https://community.gatling.io/t/wanting-to-extract-some-request-variables-hash-insert-into-header/553/9 "2013-08-29T09:47:37Z")

</div>

Ah, OK!

You have to understand how Gatling works with EL and Expressions.

Most DSL methods take Expression[T] parameters. Expression is a type alias for (Session) =\> Validation[T], meaning a function that takes a Session input and return a Validation (something that can either be a Success containing the value or a Failure containing the error message).

Then, how is it you can pass Strings to methods taking Expression[String] parameters?  
There’s a implicit conversion that happens if you pass a String to a method expecting an Expression[String]! It parses the String and resolve whatever ${} EL it might contains.

So, the point is, as the documentation states, that you can explicitly pass an Expression[String].  
If you do so, you can’t use EL there, as the implicit conversion doesn’t happen.  
So if you want to use Session attributes, you have to do it programatically.

.header(“oid”, session =\> session(“oid”).validate[String].map(myfunc))

where myfunc if a def that takes a String and returns a String.

Get it?

---

<div class="post-metadata">

**Author:** ![Dino\_Fancellu](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@Dino\_Fancellu](https://community.gatling.io/u/Dino_Fancellu)\
**Post date:** [August 29, 2013, 10:25am UTC](https://community.gatling.io/t/wanting-to-extract-some-request-variables-hash-insert-into-header/553/10 "2013-08-29T10:25:41Z")

</div>

Thanks, that made is very clear.

Should be able to do some good things with that functionality now. I understand that in using 2.x I’m rather on the bleeding edge.

You might want to add the above to the 2.,x wiki page for others.

Dino.

---

<div class="post-metadata">

**Author:** ![Dino\_Fancellu](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@Dino\_Fancellu](https://community.gatling.io/u/Dino_Fancellu)\
**Post date:** [August 29, 2013, 11:11am UTC](https://community.gatling.io/t/wanting-to-extract-some-request-variables-hash-insert-into-header/553/11 "2013-08-29T11:11:24Z")

</div>

Ok, progress:

def url:Expression[String]={

_(“oid”).validate[String].map("/myurl//"+_)  
}

later on:

.get(url)

However I also have another Expression[String] function that uses that url

def expectedHmac:Expression[String]={  
session=\>

hmacgen.generateHmac(  
url(session).toString. // how do I get the Validation to turn to a string? It comes back as Success(/theurl). I want the url  
“GET”, x\_date,  
“”).getHmac()  
}

How do I turn the Validation into a string?

---

<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:** [August 29, 2013, 11:21am UTC](https://community.gatling.io/t/wanting-to-extract-some-request-variables-hash-insert-into-header/553/12 "2013-08-29T11:21:04Z")

</div>

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def url:Expression[String]={  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_("oid").validate[String].map("/myurl//"+\_)  
> &nbsp;&nbsp;&nbsp;&nbsp;}

If I may, that's a matter of style, but I personally find the first \_  
unreadable. Matter of taste maybe.

> later on:
> 
> .get(url)
> 
> However I also have another Expression[String] function that uses that url
> 
> def expectedHmac:Expression[String]={  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session=\>
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hmacgen.generateHmac(  
> url(session).toString. // how do I get the Validation to turn to a string?  
> It comes back as Success(/theurl). I want the url  
> "GET", x\_date,  
> "").getHmac()  
> &nbsp;&nbsp;&nbsp;&nbsp;}

That's the other way around: you first get a Validation of your url, then  
map its content:

def expectedHmac:Expression[String]= { session =\> url(session).map(url =\>  
hmacgen.generateHmac(url, "GET", x\_date).getHmac) }

---

<div class="post-metadata">

**Author:** ![Dino\_Fancellu](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@Dino\_Fancellu](https://community.gatling.io/u/Dino_Fancellu)\
**Post date:** [August 29, 2013, 12:11pm UTC](https://community.gatling.io/t/wanting-to-extract-some-request-variables-hash-insert-into-header/553/13 "2013-08-29T12:11:58Z")

</div>

Ok, thought as much, that just struck me as a bit odd, having to do a map just to get the value.

Would be nice to have a method to get the content. A get/getOrElse in the Option style?

If I have to get several values from a bag of session attributes then it gets a bit abstruse.

BTW, first url could be even shorter

def url:Expression[String]=_(“oid”).map("/myurl/"+_)

Ha!

Anyway, got my stuff working this way:

def urlString:(Session=\>String)={  
“/myurl/orderid\_”+\_(“oid”).as[Integer]  
}

def url:Expression[String]=urlString(\_)

def expectedHmac:Expression[String]={  
session=\>

hmacgen.generateHmac(  
urlString(session),  
“GET”, x\_date,  
“”).getHmac()  
}

then

.get(url)

.header(“Authorization”,expectedHmac)

Thanks so much for all your help.

Dino.

---

<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:** [August 29, 2013, 12:34pm UTC](https://community.gatling.io/t/wanting-to-extract-some-request-variables-hash-insert-into-header/553/14 "2013-08-29T12:34:34Z")

</div>

> Ok, thought as much, that just struck me as a bit odd, having to do a map  
> just to get the value.

That's because you still think imperative style and not functional. With  
map, You didn't just "get the value", you converted a Validation of the url  
into a Validation of hmac without having to think about the Validation  
being a Success or a Failure and got the potential error message  
automatically conveyed for you.

> Would be nice to have a method to get the content. A get/getOrElse in the  
> Option style?

get is a big No way! It's just a dangerous stuff that might produce a NPE,  
being there for imperative developers.  
getOrElse is a no too, as you'd lose the potential error message if the  
Validation is a Failure. You can pattern match on Success and Failure if  
you want.

You have to realize that Validation potentially contains more information  
than just the success object.

> If I have to get several values from a bag of session attributes then it  
> gets a bit abstruse.

Nope, that's actually very neat:  
for {  
&nbsp;&nbsp;a \<- session("a").validation[A]  
&nbsp;&nbsp;b \<- session("b").validation[B]  
&nbsp;&nbsp;c \<- session("c").validation[C]  
} yield doSomething(a, b, c)

> BTW, first url could be even shorter
> 
> def url:Expression[String]=\_("oid").map("/myurl/"+\_)

Shorter doesn't mean more readable, coding is no character typing race 😉

> Ha!
> 
> Anyway, got my stuff working this way:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;def urlString:(Session=\>String)={  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"/myurl/orderid\_"+\_("oid").as[Integer]  
> &nbsp;&nbsp;&nbsp;&nbsp;}
> 
> def url:Expression[String]=urlString(\_)
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def expectedHmac:Expression[String]={  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session=\>
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hmacgen.generateHmac(  
> urlString(session),  
> "GET", x\_date,  
> "").getHmac()  
> &nbsp;&nbsp;&nbsp;&nbsp;}
> 
> then
> 
> .get(url)  
> .header("Authorization",expectedHmac)

Well, as[Integer] might throw exceptions (for example, if oid is missing  
for whatever reason), which would harm performance.

You're used to imperative style, and starting this way is fine, but I  
advice at some point you try to wrap your mind around functional style and  
use validate instead of as.

---

<div class="post-metadata">

**Author:** ![Dino\_Fancellu](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@Dino\_Fancellu](https://community.gatling.io/u/Dino_Fancellu)\
**Post date:** [August 29, 2013, 2:16pm UTC](https://community.gatling.io/t/wanting-to-extract-some-request-variables-hash-insert-into-header/553/15 "2013-08-29T14:16:52Z")

</div>

Ok, more idiomatic:

def url:Expression[String]={  
sess=\>  
sess(“oid”).validate[Int].map("/myurl/orderid\_"+\_)  
}

def expectedHmac:Expression[String]={  
sess=\>  
url(sess).map(hmacgen.generateHmac(\_,“GET”, x\_date,"").getHmac())  
}

---

<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:** [August 29, 2013, 2:21pm UTC](https://community.gatling.io/t/wanting-to-extract-some-request-variables-hash-insert-into-header/553/16 "2013-08-29T14:21:24Z")

</div>

Much better IMHO 🙂  
Just make them val instead of def so you don’t create a new function instance every time and you’ll be good.

---

<div class="post-metadata">

**Author:** ![Dino\_Fancellu](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@Dino\_Fancellu](https://community.gatling.io/u/Dino_Fancellu)\
**Post date:** [August 29, 2013, 2:25pm UTC](https://community.gatling.io/t/wanting-to-extract-some-request-variables-hash-insert-into-header/553/17 "2013-08-29T14:25:19Z")

</div>

Doh!

Dino.
