Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

113
Vistas
Permission issue in Axios post request

I have a back in Django using Django Rest Framework during a post request I want an authorization token to be entered in order to know if the user is logged in to validate the request. But when in my front in React native, I put in the header the authorization with the token of the user but when I validate the request by pressing the button it goes into the catch and it tells me a 401 error so that I I don't have permission. Except when I do it with postman, I log in, I retrieve the token obtained and I put it in the authorization and I launch the same request I have a 201 status created.

Here is my code in React Native Front :

const submitButton = async () => {
  if (!gardenName.trim()) {
    alert("Veuillez renter un nom de jardin s'il vous plait!");
    return;
  }
  setLoading(true);
  try {
    const jsonValue = await AsyncStorage.getItem("user");
    const parsedUserData = JSON.parse(jsonValue) || {};
    setUser(parsedUserData);

    const data = new FormData();
    data.append("user", parsedUserData.user.id);
    data.append("name", gardenName);

    const response = await axios.post(`${baseUrl}/garden/`, data, {
      headers: {
        Authorization: `Token ${parsedUserData.token}`,
        "Content-Type": "multipart/form-data",
      },
    });

    if (response.status === 201) {
      alert(`Votre jardin ${gardenName} a bien été crée`);
      setLoading(false);
      setGardenName("");
      //navigation.replace('BottomNavigation');
    } else {
      throw new Error("Erreur");
    }
  } catch (e) {
    setLoading(false);
    alert(e.message);
    console.log(e);
  }
};

I do not understand what is missing in the configuration of my Axios request, if you can enlighten me thank you!

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

did you update your INSTALLED_APPS like this;

INSTALLED_APPS = [
     ...
     'django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'rest_framework',
     'rest_framework.authtoken',
     ....
]

and also you must update your REST_FRAMEWORK settings. You should add this lines.

'DEFAULT_AUTHENTICATION_CLASSES': (
    'rest_framework.authentication.TokenAuthentication',
),
about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda