首页 > Web开发 > 详细

[Angular] Set Metadata in HTTP Headers with Angular HttpHeaders

时间:2018-02-14 21:19:40      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:strong   ever   get   etc   nbsp   response   side   end   call   

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

标签:strong   ever   get   etc   nbsp   response   side   end   call   

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

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号