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

166
Vistas
401 Error in React and django-rest-framework app

First of all I got this useState which takes data from input:

const Checkout = ({cart, totalPrice, code}) => {
    const [values, setValues] = useState({
        email: '',
        city:'',
        adress:'',
        postalCode:'',
        phone:'',
        firstName:'',
        lastName:'',
        message:'',
        price: totalPrice,
        code: code,
        shipmentFee:0,
        shipmentMethod:'',
        lockerId:'',
        cart:cart,
    });

Then I submit it with generateOrder(values) function:

export const generateOrder = (order) => {
    return fetch(`${url}/orders/generate-bank-transfer-order/`,{
        method:"POST",
        body:order
    })
    .then((response) => {
        return response.json();
    })
    .catch((error) => console.log(error))
};

It points to this url in urls.py: path('generate-bank-transfer-order/',generate_bank_transfer_order, name="generate-bank-transfer")

And this is a view I use, for now I just want it to return submited data so I can test if it works:

@csrf_exempt
def generate_bank_transfer_order(request):
    if request.method == "POST":
        body = request.body
    return JsonResponse({"test":body})

All I get is 401 Unauthorized and I have no idea how to fix it. Any ideas?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you are using the default authentication class in settings like :

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ]
}

First import

from rest_framework.permissions import AllowAny

then modify your function to

@api_view(['POST'])
@permission_classes([AllowAny])
def generate_bank_transfer_order(request):
    if request.method == "POST":
        body = request.body
    return JsonResponse({"test":body})

For More Details

The AllowAny permission class will allow unrestricted access, regardless of if the request was authenticated or unauthenticated.

This permission is not strictly required, since you can achieve the same result by using an empty list or tuple for the permissions setting, but you may find it useful to specify this class because it makes the intention explicit.

about 4 years ago · Juan Pablo Isaza 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