0

How can I add a custom header to http request using angular5? I have tried something like this.

        import {Headers} from 'angular2/http';
        var headers = new Headers();
        headers.append(headerName, value);

        // HTTP POST using these headers
         this.http.post(url, data, {
         headers: headers
          })
      // do something with the response
Supun Dharmarathne
  • 1,138
  • 1
  • 10
  • 19

1 Answers1

0

You can refer below example for Custom header:

import { Injectable }    from '@angular/core';
import { Http, Headers, Response } from '@angular/http';


@Injectable()
export class AbcService {

  constructor(private http: Http) { }

  search = (query) => {
    let headers = new Headers();
    headers.append('Api-User-Agent', 'Example/1.0');
    let apiUrl: string = 'https://en.wikipedia.org/w/api.phpgsrsearch='+query;

return this.http
        .get(apiUrl, headers)
        .map(response => response.json());
  }
}
dipendra
  • 261
  • 1
  • 3
  • 9