0

I have an authenticated API from that i want to fetch the data. I am doing this in REACT using Axios.. How to do this?

Ramya MiiM
  • 245
  • 2
  • 4
  • 11

2 Answers2

1

Something like below

const AuthString = 'Bearer '.concat(USER_TOKEN); 
axios.get(URL, { headers: { Authorization: AuthString } })
 .then(response => {
     console.log(response.data);
  })
 .catch((error) => {
     console.log('error ' + error);
  });
Hemadri Dasari
  • 32,666
  • 37
  • 119
  • 162
0

1- You can create something like this if you already have access token.

const authAxios = axios.create({
baseURL: "yourURL",
headers: {
  Authorization: `Bearer ${accessToken}`,
},

});

2- After creating the axios, you can use the created axios when hitting the API:

  authAxios.get("URL").then((res) => {
    return res.data;
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 14 '22 at 13:14