Как добавить пользовательские заголовки в HTTP-запрос Angular5?

Как я могу добавить собственный заголовок в http-запрос с помощью angular5? Я пробовал что-то вроде этого.

        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

person Supun Dharmarathne    schedule 01.06.2018    source источник
comment
Может решить вашу проблему: stackoverflow.com/questions /34464108/   -  person Rohit Sharma    schedule 01.06.2018
comment
Я проверю это. Спасибо   -  person Supun Dharmarathne    schedule 01.06.2018


Ответы (1)


Вы можете обратиться к приведенному ниже примеру для пользовательского заголовка:

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());
  }
}
person dipendra    schedule 01.06.2018