Cannot Basic Auth from React App with Axios or SuperAgent
Asked Answered
G

0

7

I try to make a GET request with axios and I always get 401. This happens only when I send the request from my react app.

axios.get('http://localhost:8080/vehicles', {
        withCredentials: true,
        auth: {
            username: 'admin',
            password: 'admin'
        },
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        },
    })

Postman GET request with the same credentials and Basic Auth set, works.

If I simply type in my browser the url, a login box appears and with the same credentials, it works, I get what I want.

I tried with SuperAgent too:

Request.get("http://localhost:8080/vehicles")
        .withCredentials()
        .auth('admin', 'admin')
        .then(response => {
            return response.body
        });

Still nothing.

I saw a question related to this one but no one answered it.

(Basic authentication : failure supergaent+OSX , success on superagent+Redhat , success on Postman+OSX,)

Can anyone point me to the right direction?

Thank you for reading.

Glycerite answered 4/1, 2017 at 16:31 Comment(3)
I feel ya, bro. I have the same issue and I'm googling like a maniac... nothing!Disestablish
i managed it somehow with superangent requesting a get request on a path where auth is needed, .withCredentials() .auth(this.state.user, this.state.password) .end((err, res) =>{});, and after that on every request just using .withCredentials(), without .auth again.Glycerite
I see. In my case the problem was on the server side, OPTIONS response didn't come with ALLOW ORIGIN header. That was the problem :)Disestablish

© 2022 - 2024 — McMap. All rights reserved.