如何将自定义标头添加到HTTP请求Angular5?

时间:2018-06-01 07:25:09

标签: angular5 angular-httpclient

如何使用angular5为http请求添加自定义标头? 我尝试过这样的事情。

        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

1 个答案:

答案 0 :(得分:0)

您可以在下面举例说明自定义标题:

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());
  }
}