# How to set dynamic value to header.

**URL:** <https://community.gatling.io/t/how-to-set-dynamic-value-to-header/3919>\
**Category:** Gatling (Open-Source)\
**Created:** [September 6, 2017, 5:34am UTC](https://community.gatling.io/t/how-to-set-dynamic-value-to-header/3919 "2017-09-06T05:34:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tracy\_Zhao](https://avatars.discourse-cdn.com/v4/letter/t/779978/32.png) [@Tracy\_Zhao](https://community.gatling.io/u/Tracy_Zhao)\
**Post date:** [September 6, 2017, 5:34am UTC](https://community.gatling.io/t/how-to-set-dynamic-value-to-header/3919/1 "2017-09-06T05:34:52Z")

</div>

HI guys:

I have one question about set dynamic vlaue to header.  
request\_6 get dynamic value and saveAs(“csrf\_token”). and request\_7 will use headers\_4, “X-CSRF-TOKEN” of headers\_4 will be replaced with {“csrf\_token”}

var headers\_4 = Map(  
“Content-Type” → “application/x-www-form-urlencoded; charset=UTF-8”,  
“X-CSRF-TOKEN” → “7e0dce98-9832-4567-8b32-109c5ae88219”,  
“X-Requested-With” → “XMLHttpRequest”)

.exec(http(“request\_6”)  
.get("/users/list")  
.headers(headers\_0)  
.check(regex(“var everbridgeCSRF\_TOKEN = ([^;]_)").saveAs(“csrf\_token”))  
)  
.exec(session =\>{  
println("_\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*”)  
println(session(“csrf\_token”).as[String])  
session  
})

.exec(http(“request\_7”)  
.post(uri1 + “/users”)  
.headers(headers\_4)  
.header(“X-CSRF-TOKEN”, “${csrf\_token}”) // codes in this line can not work.  
.formParam("\_search", “false”)  
.formParam(“nd”, “1504580184806”)  
.formParam(“size”, “25”)  
.formParam(“page”, “1”)  
.formParam(“sidx”, “lastname”)  
.formParam(“sord”, “asc”)  
.formParam(“filterRules”, “”)  
.formParam(“firstName”, “tracy”)  
.formParam(“lastName”, “”))

thanks,  
Tracy

---

<div class="post-metadata">

**Author:** ![Tracy\_Zhao](https://avatars.discourse-cdn.com/v4/letter/t/779978/32.png) [@Tracy\_Zhao](https://community.gatling.io/u/Tracy_Zhao)\
**Post date:** [September 6, 2017, 6:23am UTC](https://community.gatling.io/t/how-to-set-dynamic-value-to-header/3919/2 "2017-09-06T06:23:41Z")

</div>



---

<div class="post-metadata">

**Author:** ![Tracy\_Zhao](https://avatars.discourse-cdn.com/v4/letter/t/779978/32.png) [@Tracy\_Zhao](https://community.gatling.io/u/Tracy_Zhao)\
**Post date:** [September 8, 2017, 2:17am UTC](https://community.gatling.io/t/how-to-set-dynamic-value-to-header/3919/3 "2017-09-08T02:17:42Z")

</div>



---

<div class="post-metadata">

**Author:** ![kasichmartin](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@kasichmartin](https://community.gatling.io/u/kasichmartin)\
**Post date:** [September 19, 2017, 5:13am UTC](https://community.gatling.io/t/how-to-set-dynamic-value-to-header/3919/4 "2017-09-19T05:13:45Z")

</div>

Can somebody help here, how you can use a variable in the Header? Is that possible, i tried different solutions, but only got it to work inside the body.

---

<div class="post-metadata">

**Author:** ![Vivek](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Vivek](https://community.gatling.io/u/Vivek)\
**Post date:** [September 20, 2017, 8:54am UTC](https://community.gatling.io/t/how-to-set-dynamic-value-to-header/3919/5 "2017-09-20T08:54:25Z")

</div>

var headers\_4 = Map(  
“Content-Type” → “application/x-www-form-urlencoded; charset=UTF-8”,  
// “X-CSRF-TOKEN” → “7e0dce98-9832-4567-8b32-109c5ae88219”, Comment This line  
“X-Requested-With” → “XMLHttpRequest”)

.exec(http(“request\_6”)  
.get("/users/list")  
.headers(headers\_0)  
.check(regex(“var everbridgeCSRF\_TOKEN = ([^;]_)").saveAs(“csrf\_token”))  
)  
.exec(session =\>{  
println("_\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*”)  
println(session(“csrf\_token”).as[String])  
session  
})

.exec(http(“request\_7”)  
.post(uri1 + “/users”)  
.headers(headers\_4)  
.header(“X-CSRF-TOKEN”, “${csrf\_token}”) // codes in this line can not work.  
.formParam("\_search", “false”)  
.formParam(“nd”, “1504580184806”)  
.formParam(“size”, “25”)  
.formParam(“page”, “1”)  
.formParam(“sidx”, “lastname”)  
.formParam(“sord”, “asc”)  
.formParam(“filterRules”, “”)  
.formParam(“firstName”, “tracy”)  
.formParam(“lastName”, “”))
