How to retrieve cookie value from Request Header

I want to fetch value of ai_user and ai_session for Cookie from the request headers using gatling . how can I do it ?

Response Header:
Content-Type:application/json; charset=utf-8
Set-Cookie:sails.sid=s%3AIK92bHjEZgMlyQZWRCmscom_4xPHR7Fh.mbGwJuOOCLxlIrK%2F%2BKceTCVrtUIys281u1UfyCsM9Uo;

Request Header:
Cookie:
ai_user=omW3hhzaEIm3mX3tYBHBtI|2024-02-29T17:36:37.457Z; ai_session=WjpOcSFqzDEZhXnQRb3Knt|1709228197472|1709228197472; referrer=“/authlogin”; sails.sid=s%3AIK92bHjEZgMlyQZWRCmscom_4xPHR7Fh.mbGwJuOOCLxlIrK%2F%2BKceTCVrtUIys281u1UfyCsM9Uo

ai_user and ai_session is only present in Request Header. sails.sid is present in Response header which I am able to fetch. But I am unable to get ai_user and ai_session as this is only part of request header.

Code:
private val login = scenario(“Login Scenario”)
.exec(
http(“Login”)
.post(“path”)
.body(RawFileBody(“0003_request.json”))
)

Screenshots:

maybe Gatling HTTP protocol reference - helpers is what you need

1 Like

I tried with the below code but getting error . Actually these cookies (ai_session, ai_user, user) are part of Request header and is not available in Response header. Unable to track these cookies using gatling. Please help

Code
getCookieValue(CookieKey(“ai_session”)
.withDomain(“domain_name”)
.withPath(“/”)
.withSecure(true)
.saveAs(“ai_session_key”)

Error

[ERROR] i.g.h.a.c.GetCookieBuilder$$anon$1 - ‘getCookie-4’ failed to execute: No Cookie matching parameters domain=domain_name, path=/, name=ai_session, secure=true

Cookies don’t come from a request.
They either come from a response (set-cookie header) or are manually added in JavaScript in the front-end layer.

You must figure out how they are populated in your application.
getCookieValue will only let you fetch a cookie from the cookie jar after it was populated, either from the response of a previous request, or because it was manually added with addCookie.

Thanks for your response. I am able to set cookie now in Cookie jar and the same cookie is passed in subsequent request. But now I am getting http error code 400.

Do you know what 400 status code means?

The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (for example, malformed request syntax

I have cross checked my request body which is taking json. I dont see any issue in that. The same body params are working fine in Jmeter.

The request is not only the body, it’s also the headers!

  • make sure you’re running on Gatling 3.10.4
  • check your request HTTP headers

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.