从 get 请求中检索响应 header 值
Retrieve response header value from get request
我在从我的 .get 请求中检索 header 值时遇到问题。
在我的 API 中我有 Max-range:65
- 这个值告诉我数据库中有多少项目。因为我在 10 点之前就得到了它们,所以知道服务器上存在多少项目非常重要。我怎样才能在我的请求中获取这些信息?它看起来像:
在我的服务中我有一个方法:
public getRequest(params, refresh?: boolean): Observable<any> {
const getUrl = '/coreapi/request';
return this.http
.get(getUrl, {params})
.map((result: Obj[]) => {
for (const item of result) {
const unit = new Test(
item.id,
item.value);
this.unitSet.push(unit);
}
return this.unitSet;
});
}
在组件中我订阅了这个:
this.sub
.do(() => {
// do something
})
.distinctUntilChanged()
.switchMap((data: any) => {
return this.service.getRequest(data.httpParams, data.refresh);
})
.subscribe((result: Unit[]) => {
this.units = result;
});
---- 编辑
但我从 12 的列表中只得到这个 headers:
{"pragma" => "pragma"},
{"content-type" => "content-type"},
{"cache-control" => "cache-control"},
{"expires" => "expires"}
你知道如何获取自定义 header 吗?
我找到了答案,但在 Angular 文档中确实不清楚。
现在默认的 http 请求有默认选项:观察:'body',所以响应不会 return headers。
要获得 body & headers 我们应该将观察设置为 'response'.
所以在我的例子中我应该使用:
const observeVal = 'response'
return this.http
.get(this.apiUrl + getCurses, {params: params, observe: observeVal})
当默认为:
const observeVal = 'body'
return this.http
.get(this.apiUrl + getCurses, {params: params, observe: observeVal})
我在从我的 .get 请求中检索 header 值时遇到问题。
在我的 API 中我有 Max-range:65
- 这个值告诉我数据库中有多少项目。因为我在 10 点之前就得到了它们,所以知道服务器上存在多少项目非常重要。我怎样才能在我的请求中获取这些信息?它看起来像:
在我的服务中我有一个方法:
public getRequest(params, refresh?: boolean): Observable<any> {
const getUrl = '/coreapi/request';
return this.http
.get(getUrl, {params})
.map((result: Obj[]) => {
for (const item of result) {
const unit = new Test(
item.id,
item.value);
this.unitSet.push(unit);
}
return this.unitSet;
});
}
在组件中我订阅了这个:
this.sub
.do(() => {
// do something
})
.distinctUntilChanged()
.switchMap((data: any) => {
return this.service.getRequest(data.httpParams, data.refresh);
})
.subscribe((result: Unit[]) => {
this.units = result;
});
---- 编辑
但我从 12 的列表中只得到这个 headers:
{"pragma" => "pragma"},
{"content-type" => "content-type"},
{"cache-control" => "cache-control"},
{"expires" => "expires"}
你知道如何获取自定义 header 吗?
我找到了答案,但在 Angular 文档中确实不清楚。
现在默认的 http 请求有默认选项:观察:'body',所以响应不会 return headers。
要获得 body & headers 我们应该将观察设置为 'response'.
所以在我的例子中我应该使用:
const observeVal = 'response'
return this.http
.get(this.apiUrl + getCurses, {params: params, observe: observeVal})
当默认为:
const observeVal = 'body'
return this.http
.get(this.apiUrl + getCurses, {params: params, observe: observeVal})