Home > database >  axios Authorization username and password
axios Authorization username and password

Time:10-04

I have an api with the following documentation. I am currently using axios. I am getting an Unauthorized error

const res=axios.get("http://aaaaaaaaa.com", {
                Authorization: {
                    'Username' : '09822222222',
                    'Password' : 'sana1234'
                },
                headers:{
                    'Content-Type': 'application/json'
                }
            });

Header “Content-Type”: “application/json” The login credentials should be passed as basic-auth through Authorization header Username: “test” Password: “test”

CodePudding user response:

If this is a Basic auth you should create a token consists of username:password after that encode it with base64

const username = '09822222222';
const password = 'sana1234';

const token = `${username}:${password}`;
const encodedToken = Buffer.from(token).toString('base64');

const res=axios.get("http://aaaaaaaaa.com", {
   headers:{
     'Authorization': 'Basic '   encodedToken,
     'Content-Type': 'application/json'
   }
});
  • Related