Ramya MiiM
Ramya MiiM

Reputation: 253

How to fetch data from an Authenticated API in React using Axios?

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?

Upvotes: -1

Views: 1336

Answers (2)

Tayyab Abdul Rauf
Tayyab Abdul Rauf

Reputation: 1

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;

Upvotes: 0

Hemadri Dasari
Hemadri Dasari

Reputation: 34014

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);
  });

Upvotes: 1

Related Questions