Cannot Basic Auth from React App with Axios or SuperAgent

谁说胖子不能爱 提交于 2019-12-07 05:55:37

问题


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.

来源:https://stackoverflow.com/questions/41468803/cannot-basic-auth-from-react-app-with-axios-or-superagent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!