Reputation: 253
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
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
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