# Cannot pass cookies

**URL:** <https://community.gatling.io/t/cannot-pass-cookies/7926>\
**Category:** Gatling (Open-Source)\
**Created:** [May 9, 2023, 9:20am UTC](https://community.gatling.io/t/cannot-pass-cookies/7926 "2023-05-09T09:20:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lechf1](https://avatars.discourse-cdn.com/v4/letter/l/67e7ee/32.png) [@lechf1](https://community.gatling.io/u/lechf1)\
**Post date:** [May 9, 2023, 9:20am UTC](https://community.gatling.io/t/cannot-pass-cookies/7926/1 "2023-05-09T09:20:38Z")

</div>

Hi guys,

In BaseSimulation I have below code:

protected String url = “xxx”;  
protected HttpProtocolBuilder httpProtocol = HttpDsl.http  
.baseUrl(url)  
.inferHtmlResources(  
CoreDsl.AllowList(),  
CoreDsl.DenyList(“._\.js", "._\.css”, “._\.gif", "._\.jpeg”, “._\.jpg", "._\.ico”,  
“._\.woff", "._\.woff2”, “._\.(t|o)tf", "._\.png”, “._\.svg",  
“.detectportal\.firefox\.com.”))  
.acceptHeader("_/\*”)  
.acceptEncodingHeader(“gzip, deflate”)  
.acceptLanguageHeader(“en-US,en;q=0.9”)  
.contentTypeHeader(“application/json; charset=UTF-8”)  
.userAgentHeader(  
“Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36”);

public static ChainBuilder getAuthenticate() {  
return exec(  
http(“first endpoint”)  
.get(“xxx”)  
.header(“Content-Type”,“application/json”)  
.header(“Connection”, “close”)  
.body(StringBody(“{ \n”  
+ " "password": "xxx", \n"  
+ " "username": "xxx" \n"  
+ " }“))  
.check(status().is(200))  
.check(jsonPath(”$.csrfToken").notNull().saveAs(“csrfToken”)))  
.exec(  
http(“second endpoint”)  
.post(“xxx”)  
.header(“Content-Type”,“application/json”)  
.header(“Connection”, “close”)  
.header(“CSRF-Token”, “#{csrfToken}”)  
.check(status().is(200)))  
.exec(getCookieValue(CookieKey(“securityToken”).saveAs(“securityToken”)))  
.exec(getCookieValue(CookieKey(“JSESSIONID”).saveAs(“JSESSIONID”)));  
}

public static ChainBuilder getAnalysis() {  
return exec(http(“getAnalysis”)  
.post(“xxx”)  
.headers(HEADERS)  
.check(status().is(200)));  
}

protected static Map\<String, String\> HEADERS = Map.of(  
“Accept”, “application/json”,  
“Content-Type”, “application/json”,  
“Cookie”, “JSESSIONID= #{JSESSIONID}; securityToken= #{securityToken}”);

In TestSimulation:

ChainBuilder getAnalysis =  
getAuthenticate()  
.exitHereIfFailed()  
.forever().on(  
getAnalysis()  
.exitHereIfFailed()  
.pause(1));

ScenarioBuilder scn = scenario(“TestSimulation”).exec(getAnalysis);

{  
setUp(scn.injectOpen(atOnceUsers(1))).protocols(httpProtocol);  
}

How I can pass cookie from getAuthenticate (JSESSIONID and securityToken from second endpoint) to getAnalysis?

I read documentation about cookies but I don’t understand how I can fit it to my problem 🙂

Thanks for help

---

<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:** [May 9, 2023, 11:25am UTC](https://community.gatling.io/t/cannot-pass-cookies/7926/2 "2023-05-09T11:25:44Z")

</div>

Hi,

No idea why you’re trying to do this yourself. Maybe some misunderstanding.

Gatling handles automatically cookies for you that where set by the server (`Set-Cookie` response header).  
You only have to handle cookies manually when they are set manually by JavaScript in your real client, and you do so with the `setCookie` method, not by setting yourself the `Cookie` request header.

---

<div class="post-metadata">

**Author:** ![lechf1](https://avatars.discourse-cdn.com/v4/letter/l/67e7ee/32.png) [@lechf1](https://community.gatling.io/u/lechf1)\
**Post date:** [May 9, 2023, 11:28am UTC](https://community.gatling.io/t/cannot-pass-cookies/7926/3 "2023-05-09T11:28:57Z")

</div>

Hi,

Thx for response, I doing myself because I received 401 on getAnalysis : )

I heard that Gatling should doing cookies automatically but it doesn’t work.

When I trying the same thing in Postman, I have in header something like this

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/8/81cde56849c0af735199b958f949186ded4319a4.png)

---

<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:** [May 9, 2023, 11:54am UTC](https://community.gatling.io/t/cannot-pass-cookies/7926/4 "2023-05-09T11:54:02Z")

</div>

JSESSIONID is definitely automatically handled as it comes from a `Set-Cookie` response header from the server.

Are you sure `securityToken` comes from a `Set-Cookie` response header too?

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [May 10, 2023, 7:09am UTC](https://community.gatling.io/t/cannot-pass-cookies/7926/5 "2023-05-10T07:09:07Z")

</div>

Closed on May 10, 2023, 7:09am UTC


