码迷,mamicode.com
首页 > Web开发 > 详细

[Angular] Set Metadata in HTTP Headers with Angular HttpHeaders

时间:2018-02-14 21:30:39      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:fetch   post   for   object   common   nbsp   serve   response   tpc   

Besides sending (or requesting) the actual data to the server API, there’s also often the need to send further metadata that helps the server to correctly interpret our request. Such data is most often sent using HTTP headers. In this lesson we learn how to leverage Angular’s HttpClient to set such headers. Note that when you have to continuously send certain application headers to the backend, for each single HTTP call that is being made, you may be better served by placing them in an HTTP interceptor. 

 

import { Injectable } from ‘@angular/core‘;
import { Observable } from ‘rxjs/Observable‘;
import { HttpClient, HttpErrorResponse, HttpHeaders } from ‘@angular/common/http‘;

@Injectable()
export class PeopleService {

  constructor(private http: HttpClient) {}

  fetchPeople(): Observable<Object> {
    return this.http
      .get(‘data/people.json‘, {
        headers: new HttpHeaders().set(‘app-language‘, ‘en‘)
      });
  }

}

 

[Angular] Set Metadata in HTTP Headers with Angular HttpHeaders

标签:fetch   post   for   object   common   nbsp   serve   response   tpc   

原文地址:https://www.cnblogs.com/Answer1215/p/8448884.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!