1const res = await axios.get('https://httpbin.org/basic-auth/foo/bar', {
2 // Axios looks for the `auth` option, and, if it is set, formats a
3 // basic auth header for you automatically.
4 auth: {
5 username: 'foo',
6 password: 'bar'
7 }
8});
9res.status; // 200
1const err = await axios.
2 get('https://httpbin.org/basic-auth/foo/bar', {
3 auth: {
4 username: 'foo',
5 password: 'baz' // Bad password
6 }
7 }).
8 catch(err => err);
9err.message; // "Request failed with status code 401"
10err.response.status; // 401 "Unauthorized"