# CSRF token missing react axios and django

**URL:** <https://forum.djangoproject.com/t/csrf-token-missing-react-axios-and-django/14169>\
**Category:** Mystery Errors\
**Created:** [June 7, 2022, 9:19pm UTC](https://forum.djangoproject.com/t/csrf-token-missing-react-axios-and-django/14169 "2022-06-07T21:19:04Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![KaySRubio](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kaysrubio/32/8810_2.png) [@KaySRubio](https://forum.djangoproject.com/u/KaySRubio)\
**Post date:** [June 7, 2022, 9:19pm UTC](https://forum.djangoproject.com/t/csrf-token-missing-react-axios-and-django/14169/1 "2022-06-07T21:19:05Z")

</div>

I am trying to submit user login information with a react front end using axios or fetch, and a django back end, but cannot resolve the CSRF token missing error. I’ve used a similar solution as described here: [Django CSRF Protection Guide: Examples and How to Enable](https://www.stackhawk.com/blog/django-csrf-protection-guide/) where I ensure django sends the token using a view with @ensure\_csrf\_cookie and the browser saves the cookie. In the HTML form in React, I added  
 where csrftoken is the value of the token Q3bmH8V… When the login form is submitted, the POST request includes the header X-CSRFToken: Q3bmH8V… and the Cookie: Q3bmH8V…

In Settings.py I have CSRF\_TRUSTED\_ORIGINS = [‘[http://localhost:3000](http://localhost:3000)’]  
CSRF\_COOKIE\_NAME = “X\_CSRFTOKEN”  
CSRF\_HEADER\_NAME = ‘X\_CSRFTOKEN’

I’ve tried using fetch, and I’ve tried axios, but nothing seems to resolve this error. Please help.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 7, 2022, 11:18pm UTC](https://forum.djangoproject.com/t/csrf-token-missing-react-axios-and-django/14169/2 "2022-06-07T23:18:30Z")

</div>

It’s tough to diagnose things without seeing the actual code, but there is a difference between ‘X-CSRFToken’ and ‘X-CSRFTOKEN’ and ‘X\_CSRFTOKEN’.

I’d suggest referring to the official Django docs at [Cross Site Request Forgery protection | Django documentation | Django](https://docs.djangoproject.com/en/4.0/ref/csrf/) and being extremely careful about making sure things match.

---

<div class="post-metadata">

**Author:** ![KaySRubio](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kaysrubio/32/8810_2.png) [@KaySRubio](https://forum.djangoproject.com/u/KaySRubio)\
**Post date:** [June 8, 2022, 4:26pm UTC](https://forum.djangoproject.com/t/csrf-token-missing-react-axios-and-django/14169/3 "2022-06-08T16:26:09Z")

</div>

Thanks, based on the documentation I’ve updating the settings to be the defaults, with settings.py:  
CSRF\_COOKIE\_NAME = “csrftoken”  
CSRF\_HEADER\_NAME = ‘X-CSRFToken’

My front end is sending in the headers section:  
csrftoken=hK2ZXclKOvrPbhI5YorIZl1yH4f8YykkyHi9AV4iO877qSKplXp5VUIbl9aZaGiF  
and  
X-CSRFToken:  
hK2ZXclKOvrPbhI5YorIZl1yH4f8YykkyHi9AV4iO877qSKplXp5VUIbl9aZaGiF  
But still getting CSRF token missing error.

Using a non-login view and temporarily turning off csrf protection with #@csrf\_exempt and including print(request.META), I can see that django is receiving this:  
‘HTTP\_COOKIE’: ‘csrftoken=hK2ZXclKOvrPbhI5YorIZl1yH4f8YykkyHi9AV4iO877qSKplXp5VUIbl9aZaGiF’, ‘CSRF\_COOKIE’:  
‘hK2ZXclKOvrPbhI5YorIZl1yH4f8YykkyHi9AV4iO877qSKplXp5VUIbl9aZaGiF’  
‘HTTP\_X\_CSRFTOKEN’: ‘hK2ZXclKOvrPbhI5YorIZl1yH4f8YykkyHi9AV4iO877qSKplXp5VUIbl9aZaGiF’,

I’ve tried all kinds of combinations of settings to try to match the header and cookie names, but but with csrf protection enabled (which will be required for login) I still get the error. Is there any way to figure out what django is expecting the csrftoken and cookie to look like in the headers?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 8, 2022, 5:02pm UTC](https://forum.djangoproject.com/t/csrf-token-missing-react-axios-and-django/14169/4 "2022-06-08T17:02:37Z")

</div>

Note: I know nothing about react, axios, or fetch - as a result, some of these questions may not make any sense. I’m trying to address this from a “pure JavaScript” perspective.

You’ve verified that the token you are sending is the token you have received for the initial GET from the site? (Are you getting this from your django site, or are you getting your front end from a different location?)

Are you POSTing an AJAX-style request (JSON data) or an HTML form-style request? (And does this match what your view is expecting?)

If you’re posting this as an HTML form, there’s this from the docs:

> For all incoming requests that are not using HTTP GET, HEAD, OPTIONS or TRACE, a CSRF cookie must be present, and the ‘csrfmiddlewaretoken’ field must be present and correct. If it isn’t, the user will get a 403 error.  
> Are you currently running this using runserver (or runserver\_plus) in a development environment, or are you looking at a production environment?

(If you’re posting this as AJAX, don’t worry about it.)

---

<div class="post-metadata">

**Author:** ![KaySRubio](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kaysrubio/32/8810_2.png) [@KaySRubio](https://forum.djangoproject.com/u/KaySRubio)\
**Post date:** [June 8, 2022, 5:11pm UTC](https://forum.djangoproject.com/t/csrf-token-missing-react-axios-and-django/14169/5 "2022-06-08T17:11:19Z")

</div>

Thanks for continued help. Yes, I can see the token Django sent the front-end matches the token the front end is sending back. The front end is running on a node server localhost:3000, and Django is running on a backend server localhost:8000, and both are development environments. The POST request is being done AJAX-style with JSON data. Django doesn’t not have any errors when csrf protection is disabled, so I think it’s specific to the csrf token.

---

<div class="post-metadata">

**Author:** ![KaySRubio](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kaysrubio/32/8810_2.png) [@KaySRubio](https://forum.djangoproject.com/u/KaySRubio)\
**Post date:** [June 8, 2022, 5:25pm UTC](https://forum.djangoproject.com/t/csrf-token-missing-react-axios-and-django/14169/6 "2022-06-08T17:25:33Z")

</div>

Just an update, I finally got it to work after changing CSRF\_HEADER\_NAME = ‘HTTP\_X\_CSRFTOKEN’. Kinda strange because the django documentation suggests you don’t need the ‘HTTP\_’ part, but apparently this is needed. Thanks for your help!

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 8, 2022, 5:47pm UTC](https://forum.djangoproject.com/t/csrf-token-missing-react-axios-and-django/14169/7 "2022-06-08T17:47:56Z")

</div>

Yep, you got it. The reply I had been working on —

Ok, I always forget this little tidbit from [CSRF\_HEADER\_NAME](https://docs.djangoproject.com/en/4.0/ref/settings/#csrf-header-name)

> As with other HTTP headers in `request.META` , the header name received from the server is normalized by converting all characters to uppercase, replacing any hyphens with underscores, and adding an `'HTTP_'` prefix to the name. For example, if your client sends a `'X-XSRF-TOKEN'` header, the setting should be `'HTTP_X_XSRF_TOKEN'` .

So I think:

> [@KaySRubio](#):
>
> CSRF\_HEADER\_NAME = ‘X-CSRFToken’

should actually be:  
`CSRF_HEADER_NAME = ‘HTTP_X_CSRFTOKEN’`  
(which you’ve already discovered)

---

<div class="post-metadata">

**Author:** ![KaySRubio](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kaysrubio/32/8810_2.png) [@KaySRubio](https://forum.djangoproject.com/u/KaySRubio)\
**Post date:** [June 10, 2022, 6:05pm UTC](https://forum.djangoproject.com/t/csrf-token-missing-react-axios-and-django/14169/8 "2022-06-10T18:05:53Z")

</div>

Thanks again for your help!!!

---

<div class="post-metadata">

**Author:** ![jeferson1985](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jeferson1985/32/13497_2.png) [@jeferson1985](https://forum.djangoproject.com/u/jeferson1985)\
**Post date:** [April 29, 2023, 2:06pm UTC](https://forum.djangoproject.com/t/csrf-token-missing-react-axios-and-django/14169/9 "2023-04-29T14:06:06Z")

</div>

Good morning, I’m having the same problem, django in the back and Vue in the front.  
However the post type requests are failing, it is already sending

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 29, 2023, 2:47pm UTC](https://forum.djangoproject.com/t/csrf-token-missing-react-axios-and-django/14169/10 "2023-04-29T14:47:59Z")

</div>

@jeferson1985 Welcome!  
I would suggest you open up a new topic with a more complete description of the situation you’re facing. While you might be having the same issue, it’s as likely as not that the underlying cause is different.

---

<div class="post-metadata">

**Author:** ![Abmichael01](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/abmichael01/32/26977_2.png) [@Abmichael01](https://forum.djangoproject.com/u/Abmichael01)\
**Post date:** [February 18, 2025, 1:58am UTC](https://forum.djangoproject.com/t/csrf-token-missing-react-axios-and-django/14169/11 "2025-02-18T01:58:32Z")

</div>

I had same issue, I am using react for frontend.  
In my case, the csrftoken was saved in cookie but I could not be read with js even though “httpOnly: False” and I still don’t know why, it actually worked before. While debugging, I discovered that the csrftoken cookie is being sent with the request, so in my authenticate that by, i manually set the X-CSRFToken Header, since the frontend won’t let me.

 ![DjangoHelp1](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/5/9/59ee45829357da2eda91a520b2a721d60b493128.png)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 18, 2025, 3:27am UTC](https://forum.djangoproject.com/t/csrf-token-missing-react-axios-and-django/14169/12 "2025-02-18T03:27:20Z")

</div>

Welcome @Abmichael01 !

For future reference, please do _not_ post images of code, templates, scripts, etc.

For all text information, copy/paste the actual code, etc into the body of your post as text.

When posting code here, enclose the code between lines of three  
backtick - ` characters. This means you’ll have a line of ```, then your code,  
then another line of ```. This forces the forum software to keep your code  
properly formatted.

Thanks!
