Any ideas how can I add header to this function?

Viewed 68

Any ideas how can I add header to the function shown below? I created login service and I get in response status 200. However when I then try to send GET request I get 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 problem was that the backend was expecting Token to send in the form Token 132083128901302 however I was sending Bearer 132083128901302. After changing Bearer to Token everything works properly.

1 Answers
Related