angular - AutoRest 的 TypeScript 客户端的 HttpInterceptor
问题描述
我对 TypeScript 和 Angular 还是很陌生。我有http拦截器:
export class HttpTokenInterceptor implements HttpInterceptor {
constructor() { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const headersConfig = {
'Content-Type': 'application/json',
'Accept': 'application/json'
};
headersConfig['TEST'] = `TEST`;
const request = req.clone({ setHeaders: headersConfig });
return next.handle(request);
}
}
当我通过 HttpClient 从 '@angular/common/http' 发送 GET 请求时,let resp = await this.http.get('http://localhost').toPromise();
我可以在标题中看到我的 TEST 标题项。
我还使用 autorest 为我的休息服务生成 TypeScript 客户端(@microsoft.azure/autorest-core: 2.0.4302),并且当我调用生成的客户端自定义 TEST 标头的任何方法时,不会将其添加到查询中。
我认为 AutoRest 也通过相同的 HttpClient 生成 HTTP 查询,应该由HttpInterceptor
......
也许我这样做完全错误,它应该以这种方式工作吗?或者有没有办法拦截由 AutoRest 生成的代码发出的 http 请求?
解决方案
推荐阅读
- ios - React Native Swift UI 组件框架大小
- apache-spark - 读取带有特殊字符的 ORC Spark DataFrames
- c - 减去 C 中的无符号整数并获得 -Wconversion 警告
- editor - Adobe 的括号编辑器,运行脚本扩展,它们可以与最新的 v 一起使用吗?
- javascript - document.querySelector 选择不同的按钮
- javascript - 如何通过键访问对象属性
- c# - 试图向数据库添加一行,但它没有添加条目
- html - 为什么不列出项目大小以适合其内容?
- c# - 是否有一种 OCP 友好的方式来动态选择 DbContext 中要访问的集合?
- mysql - 如何在MySQL的其他表中查看ID是否具有特定值