# Getting CSRF token using Gatling and Laravel

**URL:** <https://community.gatling.io/t/getting-csrf-token-using-gatling-and-laravel/4458>\
**Category:** Gatling (Open-Source)\
**Created:** [August 21, 2018, 1:39pm UTC](https://community.gatling.io/t/getting-csrf-token-using-gatling-and-laravel/4458 "2018-08-21T13:39:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bruce\_Marinello](https://avatars.discourse-cdn.com/v4/letter/b/4491bb/32.png) [@Bruce\_Marinello](https://community.gatling.io/u/Bruce_Marinello)\
**Post date:** [August 21, 2018, 1:39pm UTC](https://community.gatling.io/t/getting-csrf-token-using-gatling-and-laravel/4458/1 "2018-08-21T13:39:31Z")

</div>

Last week I downloaded Gatling 2.3 to see if it could solve my need to load test a Laravel framework web app. Assuming I can get past this current problem, my company might be interested in acquiring the Enterprise version. Attached is a Gatling recording of a single update activity, BookSuite.scala. Also attached is one of several attempts to enable load testing by saving the CSRF token to a variable and sending it with each request (aBookSuite.scala). In Laravel the token remains the same throughout the session.

Also attached is a full trace log of running aBookSuite (gatling.log).

If anyone can help me with grabbing the CSRF token variable, I would be very appreciative.

Regards,  
Bruce

PS. Following is the console log:

C:\Users\Bruce\gatling\gatling-charts-highcharts-bundle-2.3.1\bin\>gatling  
GATLING\_HOME is set to “C:\Users\Bruce\gatling\gatling-charts-highcharts-bundle-2.3.1”  
JAVA = “java”  
Choose a simulation number:  
[0] RecordedSimulation  
[1] computerdatabase.BasicSimulation  
[2] computerdatabase.advanced.AdvancedSimulationStep01  
[3] computerdatabase.advanced.AdvancedSimulationStep02  
[4] computerdatabase.advanced.AdvancedSimulationStep03  
[5] computerdatabase.advanced.AdvancedSimulationStep04  
[6] computerdatabase.advanced.AdvancedSimulationStep05  
[7] suitetest.BookSuite  
[8] suitetest.TestProd1  
[9] suitetest.TestProd2  
[10] suitetest.aBookSuite  
10  
Select simulation id (default is ‘abooksuite’). Accepted characters are a-z, A-Z, 0-9, - and \_

Select run description (optional)

Simulation suitetest.aBookSuite started…

[gatling.log](https://community.gatling.io/uploads/short-url/w4nISh1OiZLoGtUrZTcuSGY49CR.log) (74.7 KB)

[aBookSuite.scala](https://community.gatling.io/uploads/short-url/oXHdjCur16KNWvUw4EsndbK2zLS.scala) (5.76 KB)

[BookSuite.scala](https://community.gatling.io/uploads/short-url/wz3X2Fw87GSIvzwYCenWgYe31YK.scala) (5.82 KB)

---

<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:** [August 21, 2018, 2:44pm UTC](https://community.gatling.io/t/getting-csrf-token-using-gatling-and-laravel/4458/2 "2018-08-21T14:44:56Z")

</div>

Your CSRF token is your HTML in a meta tag:

The best way to parse HTML is to use a [CSS selector](https://www.w3schools.com/cssref/css_selectors.asp) based [check](https://gatling.io/docs/current/http/http_check/), such as css(“meta[name = ‘csrf-token’]”, “content”). There’s a very similar example in the [advanced tutorial](https://gatling.io/docs/current/advanced_tutorial/).

Cheers,

---

<div class="post-metadata">

**Author:** ![Bruce\_Marinello](https://avatars.discourse-cdn.com/v4/letter/b/4491bb/32.png) [@Bruce\_Marinello](https://community.gatling.io/u/Bruce_Marinello)\
**Post date:** [August 21, 2018, 3:29pm UTC](https://community.gatling.io/t/getting-csrf-token-using-gatling-and-laravel/4458/3 "2018-08-21T15:29:05Z")

</div>

Thank you for your reply. I put the following code in my .scala file and received the same response error to request\_13:

.exec(http(“request\_13”)  
.post(“https://” + uri1 + “:443/49355629/requirements”)  
.headers(headers\_7)  
.check(css(“meta[name=csrf-token]”, “content”).saveAs(“stoken”))  
.formParam(“suiteRequest”, “on”)  
.formParam("\_token", “${stoken}”))

Console error:

Failed to build request request\_13: No attribute named 'stoken 1 (100.0%)  
’ is defined

Regards,  
Bruce

---

<div class="post-metadata">

**Author:** ![Flavio\_da\_Silveira\_P](https://avatars.discourse-cdn.com/v4/letter/f/aeb1de/32.png) [@Flavio\_da\_Silveira\_P](https://community.gatling.io/u/Flavio_da_Silveira_P)\
**Post date:** [August 21, 2018, 3:35pm UTC](https://community.gatling.io/t/getting-csrf-token-using-gatling-and-laravel/4458/4 "2018-08-21T15:35:04Z")

</div>

I don’t know about the response body, but I guess if you want to use the variable stoken, you need to extract it in the previous request. Watch your response body via browser.

---

<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:** [August 21, 2018, 3:37pm UTC](https://community.gatling.io/t/getting-csrf-token-using-gatling-and-laravel/4458/5 "2018-08-21T15:37:05Z")

</div>

Nah, the check is the be performed on the previous request! The one that returned the token you’re trying to inject into this one.

---

<div class="post-metadata">

**Author:** ![Bruce\_Marinello](https://avatars.discourse-cdn.com/v4/letter/b/4491bb/32.png) [@Bruce\_Marinello](https://community.gatling.io/u/Bruce_Marinello)\
**Post date:** [August 21, 2018, 3:51pm UTC](https://community.gatling.io/t/getting-csrf-token-using-gatling-and-laravel/4458/6 "2018-08-21T15:51:16Z")

</div>

Thanks again for your kind attention. I moved the saveAs up to the previous request, but am still getting the same error.

.exec(http(“request\_12”)  
.get(“/success.txt”)  
.headers(headers\_3)  
.check(css(“meta[name=csrf-token]”, “content”).saveAs(“stoken”)))  
.pause(2)  
.exec(http(“request\_13”)  
.post(“https://” + uri1 + “:443/49355629/requirements”)  
.headers(headers\_7)  
.formParam(“suiteRequest”, “on”)  
.formParam(“\_token”, “${stoken}”))

> Failed to build request request\_13: No attribute named 'stoken 1 (100.0%)  
> ’ is defined

Regards,  
Bruce

---

<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:** [August 21, 2018, 3:52pm UTC](https://community.gatling.io/t/getting-csrf-token-using-gatling-and-laravel/4458/7 "2018-08-21T15:52:37Z")

</div>

Your logs don’t match your code. You might be editing somewhere else/not saving/not properly recompiling.

---

<div class="post-metadata">

**Author:** ![Flavio\_da\_Silveira\_P](https://avatars.discourse-cdn.com/v4/letter/f/aeb1de/32.png) [@Flavio\_da\_Silveira\_P](https://community.gatling.io/u/Flavio_da_Silveira_P)\
**Post date:** [August 21, 2018, 4:12pm UTC](https://community.gatling.io/t/getting-csrf-token-using-gatling-and-laravel/4458/8 "2018-08-21T16:12:05Z")

</div>

You need to record the navigation again.  
First, disable a option in firefox, [http://forums.mozillazine.org/viewtopic.php?f=9&t=3030018](http://forums.mozillazine.org/viewtopic.php?f=9&t=3030018)  
Then you will get the correct request **s.**

---

<div class="post-metadata">

**Author:** ![Bruce\_Marinello](https://avatars.discourse-cdn.com/v4/letter/b/4491bb/32.png) [@Bruce\_Marinello](https://community.gatling.io/u/Bruce_Marinello)\
**Post date:** [August 21, 2018, 5:55pm UTC](https://community.gatling.io/t/getting-csrf-token-using-gatling-and-laravel/4458/9 "2018-08-21T17:55:19Z")

</div>

Success! Attached is the successful .scala file. I recorded the navigation using the suggested Firefox config change and things became much easier. The resulting recording was much shorter without all the “success” requests. I hope this will help all the other Laravel framework people out there when they look for similar help. Attached is the working script, bBookSuite.scala.

[bBookSuite.scala](https://community.gatling.io/uploads/short-url/aKYCwTSCOWjN53TPuU2pT8cDxr0.scala) (2.81 KB)

---

<div class="post-metadata">

**Author:** ![Flavio\_da\_Silveira\_P](https://avatars.discourse-cdn.com/v4/letter/f/aeb1de/32.png) [@Flavio\_da\_Silveira\_P](https://community.gatling.io/u/Flavio_da_Silveira_P)\
**Post date:** [August 21, 2018, 9:19pm UTC](https://community.gatling.io/t/getting-csrf-token-using-gatling-and-laravel/4458/10 "2018-08-21T21:19:54Z")

</div>

This is not a Lavarel’s problem, it’s a Firefox’s matter.
