I am making an MERN app. Inside useEffect I am making a API Call in which I am setting a autorizarion token. But I am unable to get authorization token from headers in the backend.
useEffect(()=>{
fetch(`${API}/${userId}/courses`,{
method:"GET",
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}).then(response=>{
return response.json();
}).then(data=>{
console.log(data)
})
})
If I try to console.log req.headers
in backend I am getting response as:
{ host: 'localhost:3030',
connection: 'keep-alive'
origin: 'http://localhost:3000',
referer: 'http://localhost:3000/',
}
There is no authorization property in it. But if I try to make same api call from POSTMAN I am getting authorization token in headers. What's the problem?
CodePudding user response:
You have to wrap the headers to key headers
in arguments Object.
fetch('${API}/${userId}/courses',{
method:"GET",
headers: {
'Authorization': 'Bearer ${token}',
'Content-Type': 'application/json'
}