I have a reactjs project that makes requests using API to django-rest-framework. It was working fine before, but I'm not sure what made it stop working.
I'm already using django-cors-headers.
My settings.py:
INSTALLED_APPS = [
...
'rest_framework',
"corsheaders",
]
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
...
]
CORS_ALLOW_ALL_ORIGINS = True
My reactjs request:
fetch('/api/user/', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(obj)
})
The error I got in the django terminal:
Forbidden: /api/user/
[06/Oct/2021 01:15:31] "POST /api/user/ HTTP/1.1" 403 58
Error in console and network tabs in reactjs browser:
// console error:
UserPage.js:65 POST http://localhost:3000/api/user/ 403 (Forbidden)
//network error:
Referrer Policy: strict-origin-when-cross-origin
//Preview in Networks tab:
detail: "CSRF Failed: CSRF token missing or incorrect."
UPDATE my reactjs terminal:
Local: http://localhost:3000
On Your Network: http://172.22.192.1:3000
When I open my react project using http://172.22.192.1:3000
it works fine, but using http://localhost:3000
it still can't send a POST request.