angular - 使用 .pipe() 在 Angular 中检索响应标头
问题描述
我有一个从 API 获取 HTTP 响应的方法。我正在调用的 API 现在已更改并将我需要的字符串放入 HTTP 标头中。
目前,此代码确实有效,并且不会返回错误:
// Create a Visitor JWT Token
createVisitor() {
// Create the visitor to send to API
// TODO: Capture the params of the URL and not the full URL
this.visitor = {
BrandCode: 'honey',
QueryString: 'example=qstring'
};
// Set Token as Session & Return a Success/Fail
return this.http.post(this.api + this.controller + 'createvisitor', this.visitor).pipe(
map((response: any) => {
console.log(response);
})
);
}
但是,当我查看控制台中的日志时,它只是将响应输出为没有标题的字符串。我在警卫中使用的方法的订阅部分:
// Create the token...
this.visitorService.createVisitor().subscribe(next => {
}, error => {
console.log('Create Token Error');
});
我是否需要将本地存储从 .pipe() 方法移动到该方法的订阅部分,或者无论如何我可以在 .pipe() 中执行此操作?
解决方案
您可以在管道内执行本地存储代码,如下所示。
createVisitor() {
// Create the visitor to send to API
// TODO: Capture the params of the URL and not the full URL
this.visitor = {
BrandCode: 'honey',
QueryString: 'example=qstring'
};
// Set Token as Session & Return a Success/Fail
return this.http.post(this.api + this.controller + 'createvisitor', this.visitor, {observe: 'response'}).pipe(
map((response: any) => {
console.log(response.headers.keys());// all header names
console.log(response.body); // response content
})
);
}
推荐阅读
- sql - 在 SQL 中只有外键列的表是否正确?
- pandas - 将多行缩小为一行
- javascript - 使用 Sequelize 和 SQLITE 的聚合查询
- javascript - 如何使用 iframe 使网站响应屏幕尺寸
- python - 从列表创建基于 SEQUENCE 的字典
- java - Spring Boot Sql 日期时间戳 Json 序列化问题
- sharepoint - 通过 CSOM 读取内容类型的数据
- c# - 后续 http 请求后 TempData 仍然可用吗?
- angular - 如何在 Angular 7 中处理嵌套的 HTTP 请求?
- javascript - 在 map 函数中切换条件语句