I have this code to get data from API
return this.httpClient.get<any>(requestUrl, {
headers: new HttpHeaders({
"Content-Type": "application/json",
"Authorization": "Bearer " + this.cookieService.get('auth-token'),
}),
});
when I subscribe to this like this
return this.api.loadAll('cuser').subscribe(
result => {
console.log(result);
},
);
It doesn't return any data to me and just loading
but when I remove the headers and call it again it shows me the data.
return this.httpClient.get<any>(requestUrl, {
// headers: new HttpHeaders({
// "Content-Type": "application/json",
// "Authorization": "Bearer " + this.cookieService.get('auth-token'),
// }),
});
But I need the Authorization and the token to get current user data from Django back-end.
So what make my code wrong?
Following is the Post man sample that return correct data without issue
var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTkyNTUxMTE3LCJqdGkiOiIxMGJlZWExMDQ0MmE0NmUyOGVmM2E5NTBjY2NiNTRmOSIsInVzZXJfaWQiOjF9.5XOhaXANSk4CGbh7pHqE99Qh_yxj6YuewZHFC1UScIs");
var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
fetch("http://localhost:8000/api/cuser", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
No any error just in the network it shows the pending status The back-end log during the request