Xios basic auth - how to pass user and password to the api?

Asked

Viewed 3,854 times

2

I was looking at the documentation of Xios and saw that to pass the user and password it is necessary to use:

auth: {
  username: 'janedoe',
  password: 's00pers3cret'
},

I applied the same to my reactjs code:

axios.get(store.urlBase + 'api/teste?teste=' + teste+ '&teste2=' + teste2,{},{
   auth: {
      Username: 'janedoe',
      Password: 's00pers3cret'
   }
})

but when the api arrives the username and password value is empty, which can be done?

Note: By Postman it works!

1 answer

2


In the Axios documentation, the method get receives only two parameters: the url and settings.

In your code you are passing three. Your second parameter is an empty object ({}) and Axios is using it to set the settings. So, you just call the get method as follows:

axios.get('api', {
   auth: { 
      username: 'janedoe', 
      password: 's00pers3cret' 
   }
})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.