jpadilla / django-rest-framework-jwt

JSON Web Token Authentication support for Django REST Framework
http://jpadilla.github.io/django-rest-framework-jwt/
MIT License
3.19k stars 649 forks source link

POST 401 (Unauthorized) with Axios #390

Closed tagmetag closed 6 years ago

tagmetag commented 6 years ago

I get this error when I use axios to POST data, respond: {"detail":"Authentication credentials were not provided."}. Please help me show the error: image image My VUEJS Axios code: image

Thanks in advance!

cbouvier15 commented 6 years ago

Evidently, your Headers were not sent with the request and that is why you receive:

{"detail":"Authentication credentials were not provided."}

I recommend using an axiosInstance with common information like baseURL (your API URL) and headers.

import axios from 'axios';

const axiosInstance = axios.create({
  baseURL: process.env.REACT_APP_BACKEND_HOST,
  headers: { Authorization: `Bearer ${this.$store.state.token}` },
});

export default axiosInstance;

Then, you could call it as follows:

axiosInstance({ method, url, data, headers })

tagmetag commented 6 years ago

Thank you. It works perfectly.