OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Django Rest Framework: HTTP 401 Unauthorized error

  • Thread starter Thread starter Rahmi Pruitt
  • Start date Start date
R

Rahmi Pruitt

Guest
I am django rest framework jwt token package. For some reason, it is not receiving the token credentials. I am sure I am using axios in the correct format to store a token. I am unsure if the error is frontend or backend. I am using pythonanywhere in the backend.

Settings.py

Code:
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.BasicAuthentication',
    ),
}

Frontend

Code:
handleFormSubmit(e) {
    e.preventDefault();

    if (this.getValidationState() == 'error') {
      this.setState({invalidFormSubmit: true})
      return;
    }

    axios.defaults.headers.common['Authorization'] = 'Token ' + this.props.auth.getToken();
    console.log(this.props.auth.getToken());

    var capsule = new MyForm();
    capsule.append("user", this.state.userUrl);
    capsule.append("dateToPost", this.state.dateToPost.format());
    capsule.append("image", this.state.image);
    capsule.append("caption", this.state.caption);
    capsule.append("dateToDelete", this.state.dateToPost.add(1, "d").format());

    axios.post(this.props.auth.domain + "/snapcapsule/", capsule)
    .then(() => {
       this.setState({redirect: true})
    }).catch(err =>{
        alert(err);
    })
  }

Error

{"detail":"Authentication credentials were not provided."}
<p>I am django rest framework jwt token package. For some reason, it is not receiving the token credentials. I am sure I am using axios in the correct format to store a token. I am unsure if the error is frontend or backend. I am using pythonanywhere in the backend. </p>

<p>Settings.py</p>

<pre><code>REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
),
}
</code></pre>

<p>Frontend</p>

<pre><code>handleFormSubmit(e) {
e.preventDefault();

if (this.getValidationState() == 'error') {
this.setState({invalidFormSubmit: true})
return;
}

axios.defaults.headers.common['Authorization'] = 'Token ' + this.props.auth.getToken();
console.log(this.props.auth.getToken());

var capsule = new MyForm();
capsule.append("user", this.state.userUrl);
capsule.append("dateToPost", this.state.dateToPost.format());
capsule.append("image", this.state.image);
capsule.append("caption", this.state.caption);
capsule.append("dateToDelete", this.state.dateToPost.add(1, "d").format());

axios.post(this.props.auth.domain + "/snapcapsule/", capsule)
.then(() => {
this.setState({redirect: true})
}).catch(err =>{
alert(err);
})
}
</code></pre>

<p>Error</p>

<p>{"detail":"Authentication credentials were not provided."}</p>
 

Latest posts

Top