I am creating an API based on Flask (extended with flask-restful) and a frontend appliation with Vue.js.
Both are running on localhost for now.
flask: 127.0.0.1:5000
,
vue: localhost:8080/
I call the API using axios. The first call resulted in an CORS error, so i installed the flask-CORS extension and the CORS-error disappeared.
API-wise, the URL http://127.0.0.1:5000/signin
can be called via the GET method and requires Basic Auth authorization. If the auth data is valid, a token is returned. In Postman this works perfectly fine. In my Vue.js application calling the API via axios like this:
signIn() {
const session_url = 'http://127.0.0.1:5000/signin'
axios.get(session_url, {}, {
auth: {
username: this.email,
password: this.password
}
}).then(response => {
console.log('Authenticated')
console.log(response.data)
/*this.$store.dispatch('signin', response.data)*/
}).catch(error => {
console.log('Error on Authentication')
this.error = error
console.log(error)
});
}
... the console in the browser throws: Error: "Request failed with status code 401"
.
In Postman the API call is working fine, as well as using CURL:
curl -u Hans:testing -i -X GET http://127.0.0.1:5000/signin
returns
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 180
Access-Control-Allow-Origin: *
Server: Werkzeug/0.14.1 Python/3.6.4
Date: Sat, 22 Sep 2018 14:22:37 GMT
{
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...."
}
Why do i get the 401 error when calling the api via axios?
Thanks for your help!