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

1,188 views
Skip to first unread message

Adam Giemza

unread,
Jan 14, 2015, 2:15:44 PM1/14/15
to gat...@googlegroups.com
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(http("request_portal-entry")
.get("/
portal/secured/template")
.headers(Map("X-XSRF-TOKEN" -> "${xsrfToken}")))

The problem is that the xsrfToken is URL encoded. I tried to call URLDecoder.decode but I simply don't know how to retrieve the value of the token from the session. The ${} expression is resolved somewhere deeper in the framework so I cannot simply pass this expression to the decode method.

Any ideas?

Cheers,
 Adam

John Arrowwood

unread,
Jan 14, 2015, 2:49:36 PM1/14/15
to gat...@googlegroups.com
.exec( session => {  session.set( "decoded", decode_function( session("encoded").as[String] ) ) } )

Does that help?

Stéphane LANDELLE

unread,
Jan 14, 2015, 3:19:25 PM1/14/15
to gat...@googlegroups.com
.header("X-XSRF-TOKEN", session => session("xsrfToken").validate[String].map(URLDecoder.decode)))

Stéphane Landelle
Lead developer


--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Adam Giemza

unread,
Jan 14, 2015, 3:26:07 PM1/14/15
to gat...@googlegroups.com
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")))

Peter Oxenham

unread,
Sep 15, 2015, 12:22:02 AM9/15/15
to Gatling User Group
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

Servet Kurt

unread,
Jul 25, 2016, 7:39:45 AM7/25/16
to Gatling User Group
Thank you Peter! The way you explained worked for me.

15 Eylül 2015 Salı 07:22:02 UTC+3 tarihinde Peter Oxenham yazdı:
Reply all
Reply to author
Forward
Message has been deleted
0 new messages