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?
Asked
Active
Viewed 1,178 times
0
-
Most commonly this is done through the `Authorization` header – Alexander Derck Sep 27 '18 at 09:52
-
Could you please explain clearly i am new bie so..or else can you share the where i can get the clear idea of this?? – Ramya MiiM Sep 27 '18 at 09:53
2 Answers
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
-
I found the Bearer ans User Token in Post man..What those things are actually? – Ramya MiiM Sep 27 '18 at 10:12
-
Check this https://stackoverflow.com/questions/25838183/what-is-the-oauth-2-0-bearer-token-exactly/25843058 – Hemadri Dasari Sep 27 '18 at 10:23
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