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.

Community
  • 1
  • 1
Alexandru Hodis
  • 372
  • 4
  • 14
  • I feel ya, bro. I have the same issue and I'm googling like a maniac... nothing! – zkvarz Dec 08 '17 at 08:07
  • 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. – Alexandru Hodis Dec 08 '17 at 12:59
  • 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 :) – zkvarz Dec 27 '17 at 08:51

0 Answers0