【发布时间】:2018-07-18 21:42:15
【问题描述】:
我正在尝试学习如何使用 HttpInterceptor 为应用程序对 API 执行的每个 HTTP 请求添加几个标头。我有这个拦截器:
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class fwcAPIInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const authReq = req.clone({
headers: req.headers.set('Content-Type', 'application/json')
});
console.log('Intercepted HTTP call', authReq);
return next.handle(authReq);
}
}
除了'Content-Type' 标头之外,我还需要添加一个'Authorization',但我不知道怎么做(Angular HttpHeaders 的文档只是方法列表,没有任何解释)。
我该怎么做?谢谢!
【问题讨论】:
标签: angular angular-http-interceptors angular-httpclient