0

what is the right way to pass headers to http get request. I am using ionic 3 framework and getting this error "Expected 0 type arguments, but got 1". My function returns an observable of type IMessage.

import { Http, Headers, RequestOptions } from '@angular/http';


getChats(token:string):Observable<IMessage[]>{ 
    let headers = new Headers({
        'Content-Type' : 'application/json',
        'Authorization': 'Bearer'+ token
    });
    let options = new RequestOptions({ headers: headers });
    return this.http.get<IMessage[]>(this.url, options);
}

Note that when I return an observable of type any the request succeed.

3
  • what version of Angular you are using? Commented Jun 15, 2018 at 0:15
  • angular 4 @SergeyRudenko Commented Jun 15, 2018 at 0:21
  • Depending on which specific v4 - you may be able to use HttpClient: v4.angular.io/api/common/http/HttpClient Commented Jun 15, 2018 at 0:44

1 Answer 1

1

If you are using Angular 4.4.7+ with Ionic 3, you should leverage HttpClient instead of Http:

import { HttpClient, HttpHeaders } from '@angular/http';
...

getChats(token:string):Observable<IMessage[]>{ 
    let headers = new HttpHeaders().set('Content-Type' : 'application/json').set("Authorization", 'Bearer '+ token);
    return this.http.get<IMessage[]>(this.url, { headers });
}

I guess you need to use HttpOptions if aside headers you have more stuff in the request.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.