<- Test Directory

Login with CSRF Token

Support CSRF tokens, for example with the Laravel framework.


A test which posts to a login page when it starts, then requests /hello and /world normally. However, it also specifically handles getting a CSRF token for logging in with (in this example) Laravel.

Laravel automatically checks for a CSRF token when you submit data (by default). CSRF is designed to stop cross-site scripting against your site, and involves having a temporary token on each page that is submitted with every post. You'll know you've hit this issue if you receive an HTTP 419 error.

The below example can also be used for many other frameworks, or altered to suit them.


from locust import HttpUser, task, between
from pyquery import PyQuery

class QuickstartUser(HttpUser):
    # Wait between 5 and 9 seconds per request per user
    wait_time = between(5, 9)

    def on_start(self):
        response = self.client.get("/login")

        pq = PyQuery(response.content)
        elements = pq("input[name=_token]")
        for token in elements:
          csrftoken = token.value

        # debug example
        #print("my token is:", csrftoken)

                         {"email": "user@domain.com", "password": "secr3t", "_token" : csrftoken})

    def index_page(self):
        # Request /dashboard on your Host

This guide is part of the LoadForge Directory, an index of locustfile's for use with LoadForge website and API load tests. We also provide a wizard to generate tests, and onboarding assistance for clients. Contact us should you have any questions.

LoadForge provides load testing and stress tests for websites, APIs and servers. It uses your cloud account to rapidly scale large numbers of simulated users to load test your website, store, API, or application for cheap - just cents per test!

For more help on Tests please see our official documentation. Logged in users can also use our wizard to generate a locustfile, or you can record your browser steps.

Ready to run your test?
Start your first test within minutes.