Как читать заголовки из сообщений axios в django?

Я отправляю сообщение axios в мое приложение django backend:

async function sendBookingRequest() {
    // console.log(date,bookedHours,cancha,cellphone)
    try {
        axios.post("https://danilo2588.pythonanywhere.com/book", {
            headers: {'Authorization':"1234567890123456789012345678901234567890"},
            params:{
                'requested_date':date,
                'hours':bookedHours,
                'business':cancha,
                'phone':cellphone,
                }
        })
        .then( function(response){
            setConfirmation(response.data)
            setStepper(7)
        })
        .finally(
            setIsLoading(false)
        )
    } catch(error){
        console.log(error)
    }; }

В моих взглядах есть:

def booking(request):
    auth_key = request.headers.get('Authorization')

    if auth_key:
        generic_user = Token.objects.get(key=auth_key).user

        if request.method == "POST" and generic_user:
            #do whatever needed...

Однако представление не читает заголовки и выдает ошибку. Я мог бы просто удалить строку Authentication/token и вуаля! но дело в том, что я хочу сохранить код настолько безопасным, насколько это возможно.

Что не так с моим кодом?

Надеюсь, эта кодовая база поможет вам решить проблему.

async function sendBookingRequest() {
    setIsLoading(true); // Start loading before making the request
    try {
        const response = await axios.post(
            "https://danilo2588.pythonanywhere.com/book",
            {
                requested_date: date,
                hours: bookedHours,
                business: cancha,
                phone: cellphone,
            },
            {
                headers: {
                    Authorization: "1234567890123456789012345678901234567890",
                },
            }
        );
        setConfirmation(response.data); // Handle successful response
        setStepper(7);
    } catch (error) {
        console.log(error); // Log or handle the error
    } finally {
        setIsLoading(false); // Stop loading after the request
    }
}

А для настройки axios вы можете обратиться к этой документации. https://axios-http.com/docs/post_example

Спасибо

Вернуться на верх