首页 > Web开发 > 详细

[Angular] Set Metadata in HTTP Headers with Angular HttpHeaders

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!