axios get req with basic auth and error

Solutions on MaxInterview for axios get req with basic auth and error by the best coders in the world

showing results for - "axios get req with basic auth and error"
Juan
18 Oct 2020
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
Julian
09 Jan 2018
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"