'Axios not sending csrf token to django backend even after trying all suggested configs

The function that handles my signup make api requests with axios. Below are the configs that I have made lerning from SO.

react

import axios from "axios";
axios.defaults.xsrfHeaderName = "X-CSRFTOKEN";
axios.defaults.xsrfCookieName = "csrftoken";
axios.defaults.withCredentials = true;


export const signup =
  ({ name, email, password, password2 }) =>
  async (dispatch) => {
    const config = {
      headers: {
        "Content-Type": "application/json",
      },
    };

    const body = JSON.stringify({ name, email, password, password2 });

    try {
      const res = await axios.post(`${host}/api/account/signup/`, body, config);

      dispatch({
        type: SIGNUP_SUCCESS,
        payload: res.data,
      });

      dispatch(login(email, password));
    } catch (err) {
      dispatch({
        type: SIGNUP_FAIL,
      });

      dispatch(setAlert("Error Authenticating", "error"));
    }
  };

settings.py

CSRF_COOKIE_NAME = "csrftoken"
CSRF_COOKIE_HTTPONLY = False

CORS_EXPOSE_HEADERS = ["Content-Type", "X-CSRFToken"]
CORS_ALLOW_CREDENTIALS = True

Even after all the above configs I am getting this error in django

Forbidden (CSRF cookie not set.): /api/account/signup/
[13/Mar/2022 14:41:50] "POST /api/account/signup/ HTTP/1.1" 403 2870

Please help me to rectify this error



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source