Search code examples
djangoauthenticationdjango-rest-frameworkaxiosdjango-rest-auth

How to use axios to post to my Django Rest server?


I am getting a 403 error when trying to post to my Django server from my frontend mobile app. However, it works totally fine when I post with the form on the browser.

I managed to register once using my frontend mobile app, but ever since then, I get this 403 error. I've tried both signing up and logging in.

Here is the error I get in my Django backend terminal:

Bad Request: /rest-auth/registration/
[16/Aug/2021 14:51:37] "POST /rest-auth/registration/ HTTP/1.1" 403 58

Here is my settings.py file:


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',
    'rest_auth',

    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'rest_auth.registration',

    'users',
]

SITE_ID = 1

....


REST_FRAMEWORK = {

    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.BasicAuthentication',
    ),
}

ACCOUNT_EMAIL_VERIFICATION = 'none'


Here is my front end (register.js):

    axios
      .post("http://127.0.0.1:8002/rest-auth/registration/", {
        username: "[email protected]",
        email: "[email protected]",
        password1: "[email protected]",
        password2: "[email protected]",
      })

What am I doing wrong? I just want users to be able to register, log in, and log out of my app.


Solution

  • The issue was that I had logged in, and then was not providing the key.

    To solve this, I created a logout button, then added a mechanism to save the key and use it in the header.