Problems with adding XSRF-TOKEN from Cookies into the Request header

Dear all,

I have a problem setting the XSRF token into the header. I can successfully parse it from the headers and save in the user session as follows

exec(http("request_secured")
  .get("/portal/secured/")
  .check(headerRegex("Set-Cookie", """XSRF-TOKEN=(.*)\s""").exists.saveAs("xsrfToken")))

Later on I want to use it in a request set in the header.

.exec( session => { session.set( “decoded”, decode_function( session(“encoded”).as[String] ) ) } )

Does that help?

.header("X-XSRF-TOKEN", session =>
session("xsrfToken").validate[String].map(URLDecoder.decode)))

*Stéphane Landelle*
*Lead developer*
slandelle@gatling.io

Dear John, thanks for the quick response. In the meantime I found the filter solution (the dropRight is for removing a tailing ; (semicolon)):

.check(headerRegex("Set-Cookie", """XSRF-TOKEN=(.*)\s""").transform(token => decode(token, "UTF-8").dropRight(1)).saveAs("xsrfToken")))

A better solution is to simply grab the XSRF-TOKEN from the cookies.

`
object Helper{

def setXsrfHeader(session:Session): Validation[String] = {
getCookie(“XSRF-TOKEN”, session).map(c => URLDecoder.decode(c, “UTF-8”)) match {
case Some(value) => Success(value)
case None => Failure(“Unable to find XSRF-TOKEN cookie”)
}
}

def getCookie(name:String, session:Session): Option[String] = {
val cookieJar = session(“gatling.http.cookies”).as[CookieJar]
cookieJar.get(Uri.create(baseUrl)).find(cookie => cookie.getName == name).map(_.getValue)
}
}

`

Then use it…

`

http(“POST”)
.post("/myurl")
.header(“X-XSRF-TOKEN”, Helper.setXsrfHeader)

`

H

Thank you Peter! The way you explained worked for me.

15 Eylül 2015 Salı 07:22:02 UTC+3 tarihinde Peter Oxenham yazdı: