# Getting value from session to use in non gatling DSL function

**URL:** <https://community.gatling.io/t/getting-value-from-session-to-use-in-non-gatling-dsl-function/3421>\
**Category:** Gatling (Open-Source)\
**Created:** [December 1, 2016, 12:54pm UTC](https://community.gatling.io/t/getting-value-from-session-to-use-in-non-gatling-dsl-function/3421 "2016-12-01T12:54:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henrik\_Larsson](https://avatars.discourse-cdn.com/v4/letter/h/ecd19e/32.png) [@Henrik\_Larsson](https://community.gatling.io/u/Henrik_Larsson)\
**Post date:** [December 1, 2016, 12:54pm UTC](https://community.gatling.io/t/getting-value-from-session-to-use-in-non-gatling-dsl-function/3421/1 "2016-12-01T12:54:14Z")

</div>

I want to do something like the following where I can extract the value from my session and pass it to my own function to generate the correct headers from this simulation. However the code below does not work as expected since the http request is never executed.  
How can I do what im trying to du using gatling?

```auto
def post()(implicit h: String => Map[String, String]) =
  exec(Body.*requestBodyGenerator*).
    exec(session => {
val id = session("personId").asOption[String].getOrElse(defaultId)
      http("GetID").
        post("/getid").
        body(StringBody("${body}")).
        asJSON.
        headers(h(label)).
        check(responseTimeInMillis)
session
    })

```

---

<div class="post-metadata">

**Author:** ![Henrik\_Larsson](https://avatars.discourse-cdn.com/v4/letter/h/ecd19e/32.png) [@Henrik\_Larsson](https://community.gatling.io/u/Henrik_Larsson)\
**Post date:** [December 1, 2016, 12:55pm UTC](https://community.gatling.io/t/getting-value-from-session-to-use-in-non-gatling-dsl-function/3421/2 "2016-12-01T12:55:36Z")

</div>

label should be id in function call but this is not the problem, this is only a result of me editing the post.

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [December 1, 2016, 3:11pm UTC](https://community.gatling.io/t/getting-value-from-session-to-use-in-non-gatling-dsl-function/3421/3 "2016-12-01T15:11:24Z")

</div>

You get it wrong.  
Instead of trying to generate a RequestBuilder in a function (which doesn’t work as explained in the doc), why don’t you pass a function to headers?

---

<div class="post-metadata">

**Author:** ![Henrik\_Larsson](https://avatars.discourse-cdn.com/v4/letter/h/ecd19e/32.png) [@Henrik\_Larsson](https://community.gatling.io/u/Henrik_Larsson)\
**Post date:** [December 2, 2016, 8:37am UTC](https://community.gatling.io/t/getting-value-from-session-to-use-in-non-gatling-dsl-function/3421/4 "2016-12-02T08:37:35Z")

</div>

You mean somthing like this? This actually works but i loose the ability to use getOrElse to set a default value if no personId is added in the session.

```auto
def post()(implicit h: String => Map[String, String]) =
  exec(Body.*requestBodyGenerator*).
    exec(session => {
      http("GetID").
        post("/getid").
        body(StringBody("${body}")).
        asJSON.
        headers(h("${personId}")).
        check(responseTimeInMillis)
session
    })

```

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [December 2, 2016, 8:39am UTC](https://community.gatling.io/t/getting-value-from-session-to-use-in-non-gatling-dsl-function/3421/5 "2016-12-02T08:39:27Z")

</div>

> You mean somthing like this?

No I don't.

[http://gatling.io/docs/2.2.3/advanced\_tutorial.html#step-05-check-and-failure-management](http://gatling.io/docs/2.2.3/advanced_tutorial.html#step-05-check-and-failure-management)

---

<div class="post-metadata">

**Author:** ![Henrik\_Larsson](https://avatars.discourse-cdn.com/v4/letter/h/ecd19e/32.png) [@Henrik\_Larsson](https://community.gatling.io/u/Henrik_Larsson)\
**Post date:** [December 2, 2016, 8:52am UTC](https://community.gatling.io/t/getting-value-from-session-to-use-in-non-gatling-dsl-function/3421/6 "2016-12-02T08:52:01Z")

</div>

Still not getting it, I have tried this but headers does not support functions to be sent to it…

```auto
def post()(implicit h: String => Map[String, String]) =
  exec(Body.*requestBodyGenerator*).
    exec(session => {
      http("GetID").
        post("/getid").
        body(StringBody("${body}")).
        asJSON.
        headers(session => Map("" -> "").
        check(responseTimeInMillis)
session
    })

```
