[Angular] Read Custom HTTP Headers Sent by the Server in Angular
2024-08-25 03:00:59
By default the response body doesn’t contain all the data that might be needed in your app. Your server might return some special header which you have to read explicitly. In such case we can use the { observe: ‘response’}
configuration of the Angular HttpClient
. Let’s explore how.
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { HttpClient, HttpResponse } from '@angular/common/http'; export interface Person {
name: string;
} @Injectable()
export class PeopleService { constructor(private http: HttpClient) {} fetchPeople(): Observable<HttpResponse<Person>> {
return this.http
.get<Person>('data/people.json', { observe: 'response'});
}
}
Now instead of just returning your data, it returns your response object.
{
"headers": {
"normalizedNames": [],
"lazyUpdate": null
},
"status": 200,
"statusText": "OK",
"url": "https://run.plnkr.co/preview/cjdn2x8fh000ffillqi8d3o4k/data/people.json",
"ok": true,
"type": 4,
"body": [
{
"name": "xxx"
},
{
"name": "xxx"
}
]
}
最新文章
- [LeetCode] Implement Trie (Prefix Tree)
- oracle sqlplus 格式化输出
- @weakify, @strongify ObjC的Block中使用weakSelf/strongSelf @weakify/@strongify
- (原)java中对象复制、==、equals
- Intent的七大属性
- 分布式架构高可用架构篇_01_zookeeper集群的安装、配置、高可用测试
- 【IOS】2.基础
- 开源项目:网页实时通信WebRTC
- jdbc内容
- java改变图片文件尺寸
- ios 视频/图片压缩
- springMVC源码分析--AbstractHandlerMethodMapping获取url和HandlerMethod对应关系(十)
- 实用的几个JS新特性(es 2016)
- POJ2728 Desert King
- list集合去除重复对象的实现
- Sql server bulk insert
- Thinkphp5笔记六:公共模块common的使用
- 安卓秘钥生成命令以及SHA1值获取办法
- javascript面向对象的程序设计之Object.getOwnPropertyDescriptor()
- 团队博客作业Week2 --- 学长学姐访谈录