Can anyone advise on how to include a header in the function below? I have created a login service and receive a status of 200 in response. However, when attempting to send a GET request afterwards, I encounter the error message
Failed to load resource: the server responded with a status of 403 (Forbidden).
func() {
return this.http.get('/data', )
.map(response => response.json())
.subscribe(response2 => this.response2 = response2);
}
My GET:
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
return super.get(appConfig.apiUrl + url, this.addJwt(options)).catch(this.handleError);
}
private addJwt(options?: RequestOptionsArgs): RequestOptionsArgs {
// ensure request options and headers are not null
options = options || new RequestOptions();
options.headers = options.headers || new Headers();
// add authorization header with jwt token
let currentUser = JSON.parse(localStorage.getItem('currentUser'));
if (currentUser && currentUser.token) {
options.headers.append('Authorization', 'Bearer ' + currentUser.token);
}
return options;
}
SOLUTION
The issue was that the backend expected the Token format to be sent as Token 132083128901302
, but I was sending it as Bearer 132083128901302
. After changing from Bearer to Token, everything started working correctly.