How to set this.http.get Header

#1

I’ve searched and tested a lot of code but still not able to set header for http get request.
Here is my sample code:

  1. import
    import { Http, Headers } from '@angular/http';
  2. inside constructor
  constructor(public navCtrl: NavController, public http: Http, public headers: Headers) {
       this.headers.append('Content-Type': 'application/json' );
       this.headers.append('more_header_option', 'value');

       this.http.get('http://some_url.com/api/get_json_feed', {headers: this.headers})
                  .subscribe((res) => { this.data = res.json(); }); 

 }

I’m gettting below error in the console

error TS2304: Cannot find name 'RequestOptions'.

Any help, highly appreciated …thank you

#2

You shouldn’t inject Headers into your constructor, and you need to pass RequestOptions to http.get. Example straight from the HTTP Client docs:

  addHero (name: string): Observable<Hero> {
    let body = JSON.stringify({ name });
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    return this.http.post(this.heroesUrl, body, options)
                    .map(this.extractData)
                    .catch(this.handleError);
  }
#3

Thanks for the help …I think I’m almost there, but still getting some errors

Return type of public method from exported class has or is using name 'Observable' from external module "C:/ionic2/technotip/node_modules/rxjs/Observable" but cannot be named.

#4

a request returns an observable. if you are using additional operators oder observable functions like .map you need to import them.

import 'rxjs/add/operator/map'
#5

I’ve added / imported it, still I’m getting the same error.

#6

See if this helps: [Solved] Ionic RC0: error TS4053: Return type of public method from exported class has or is using name 'Entry' from external module

#7

Followed it and now getting ‘No provider for Headers!’ error message :frowning:

#8

hi@technotip
Did you found the solution.New to ionic :neutral_face:
Thanks,