fefe
fefe

Reputation: 9055

angular2 http request with headers, do not send

Quite new to angular2 I try to build a data service and I want to add to each request headers

here is what I try but the headers won't be sent

import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import 'rxjs/add/operator/map'
import { Observable } from 'rxjs/Observable';
import { Configuration } from '../app.constants';
import { AuthenticationService } from '../services/';

@Injectable()
export class DataService {

    private actionUrl: string;
    private headers: Headers;
    private token;


    constructor(private _http: Http,
                private _configuration: Configuration

                //private authenticationService: AuthenticationService

    ) {

        var currentUser = JSON.parse(localStorage.getItem('currentUser'));

        if(currentUser)
            this.token = currentUser.token;

        this.actionUrl = _configuration.ServerWithApiUrl + 'patients';

        this.headers = new Headers();

        this.headers.append('Content-Type', 'application/x-www-form-urlencoded');
        this.headers.append('Accept', 'application/json');
        this.headers.append('Authorization', 'Bearer ' + this.token);
    }

    public GetAll = (): Observable<any> => {
        return this._http.get(this.actionUrl, this.headers).map((response: Response) => <any>response.json());
    }
}

how to do it properly or why this do not work?

Upvotes: 0

Views: 1239

Answers (1)

John Baird
John Baird

Reputation: 2676

You need to surround your this.headers with "{}"... Like this:

return this.http.get(this.constants.accountLocalUrl, { headers: headers })

Upvotes: 1

Related Questions