moustafa
moustafa

Reputation: 271

Sending http request in ionic 3/typescript

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.

Upvotes: 0

Views: 356

Answers (1)

Sergey Rudenko
Sergey Rudenko

Reputation: 9227

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.

Upvotes: 1

Related Questions