Bobcares

How to Fix CSRF Token Mismatch in Laravel and Postman

by | Jul 3, 2024

Learn how to fix CSRF Token Mismatch in Laravel and Postman. Our Laravel Support team is here to help you with your questions and concerns.

How to Fix CSRF Token Mismatch in Laravel and Postman

When working with Laravel APIs protected by CSRF middleware, we may run into a “CSRF token mismatch” error in Postman.

This error lets us know that the CSRF tokens on the client-side (Postman) and the server-side (Laravel) are not aligned. We can easily fix this issue with a little help from opur Experts.

How to Fix CSRF Token Mismatch in Laravel and Postman

In fact there are two primary ways to resolve the CSRF token mismatch in Postman.

Disable CSRF for Testing

First, temporarily disable the VerifyCsrfToken middleware for specific routes during testing. This is done by opening `app/Http/Middleware/VerifyCsrfToken.php` and adding the route to the `$except` array within the middleware definition.

Our Experts would likr to point put that disabling CSRF protection in a production environment exposes our application to potential CSRF attacks.

So, use approach only for development or testing purposes. Also, remember to re-enable CSRF protection before deploying to production.

Include the CSRF Token in Postman Requests

  1. First, make a GET request to the `/sanctum/csrf-cookie` endpoint. So, if we are using Laravel Sanctum for SPA authentication, this endpoint will provide a cookie containing the CSRF token.
  2. Then, extract the CSRF token value from the response cookie headers. This is done by going to Postman’s “Cookies” tab or browser developer tools to view the cookies.
  3. Now, it is time to include the Token in Postman Requests. So, add a new header named `X-XSRF-TOKEN` (or `XSRF-TOKEN`, based on the Laravel configuration) to the POST request in Postman.
  4. Also, set the header value to the CSRF token we got from the `/sanctum/csrf-cookie` endpoint response.

Additional Tips

  • If we are using a JavaScript framework like Axios, it will automatically handle CSRF by including the CSRF token from cookies in the `X-XSRF-TOKEN` header. Depending on the setup, we may not need to include it in Postman requests.
  • Laravel API routes usually don’t enforce CSRF protection as they are intended for API consumption, not form submissions. So, if we are building a fully-fledged SPA using Laravel Sanctum, CSRF protection is usually managed on the SPA side.

By including the CSRF token in Postman requests, we can ensure that our API interactions remain secure and aligned with Laravel’s CSRF protection mechanism.

Disabling CSRF protection should be done only in development and testing scenarios with the correct safeguards re-enabled before moving to production.

[Need assistance with a different issue? Our team is available 24/7.]

Conclusion

In brief, our Support Experts demonstrated how to fix CSRF Token Mismatch in Laravel and Postman.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF