JWT transfer in headers with axioms

I created a small project with node back-end and reacted front-end to get data through the REST calls that I used in Axios . But when I pass the headers with it, I keep getting the error message Failed to load resource: the server responded with a status of 401 (Unauthorized) . I learned two methods, and both of them did not work.

They

 export const getUsersDetails=()=>{ console.log('calling'); return (dispatch) => { return axios.get('http://localhost:3030/users',{headers: { "Authorization": localStorage.getItem('jwtToken') }}).then((data)=>{ console.log('data comming',data); dispatch(getUsersData(data)); }).catch((error)=>{ console.log('error comming',error); dispatch(errorgetUsersData(error)); }); }; } 

and

 axios.defaults.headers.common['Authorization'] = localStorage.getItem('jwtToken'); 

But when I use the postman, I get the necessary data from the backend . Any specific reason why im keep getting this Unauthorized Error ?.

+5
source share
1 answer

You need to combine the "carrier" in front of the token, for example:

 axios.defaults.headers.common['Authorization'] = 'Bearer ' + localStorage.getItem('jwtToken'); 
+16
source

Source: https://habr.com/ru/post/1268263/


All Articles