I want to send token through header how can i do that
public details(token){
const httpOptions = {
headers: new HttpHeaders({
'Accept': 'application/json',
'Authorization':'Bearer'+token
})
};
return this.http.post('http://mywebsite.ca/helpify/api/details', httpOptions)
}
whenever i tired to hit this api its showing 401 Unauthorized
how can i fix it please help me out
Thanks in advance
feaxer
#2
Like this.
Authorization’:'Bearer '+token
You must check request. from chrome
must look like this http://prntscr.com/lyzmzy
Thanks for your response
yes i tired but showing same problem 401 unauthorized
Actually am using laravel api
public function details(Request $request)
{
$user = Auth::user();
$acceptHeader = $request->header('Accept','Authorization');
if ($acceptHeader != 'application/json') {
return response()->json([], 400);
}
return response()->json(['success' => $user], $this-> successStatus);
}
this is my console
Thanks
feaxer
#4
bro, you sent data. not header.
You must fixed İonic Code like this.
http.post(‘http://mywebsite.ca/helpify/api/details’, data, httpOptions)
syntax
http.post('APİ URL,DATA,HTTPOPTİONS)
if you dont want data.
http.post('APİ URL,NULL,HTTPOPTİONS)
1 Like
Thanks for your response please let me try
This is my full code
public details(token){
const headers = new Headers();
headers.append('Content tyepe','application/x-www-form-urlencoded')
headers.append('Accept', 'application/json' );
headers.append('Authorization', 'Bearer ' + token); //<-- added space after 'Bearer'
const options = new RequestOptions({ headers: headers });
return this.http.post('http://mydomain.ca/helpify/api/details', options)
}
@feaxer above code right or wrong?
var data = null;
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "http://mywebsite.ca/helpify/api/details");
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Authorization", "Bearer "+token);
xhr.setRequestHeader("cache-control", "no-cache");
xhr.setRequestHeader("Postman-Token", "f5009fbf-3bc2-4f58-ace0-b767f7a6092c");
xhr.send(data);
this above code is working fine but i i want to do in angular