有没有办法拦截FetchBackend的Fetch请求以添加授权头?
我曾try 使用HttpInterceptor拦截这些FETCH请求,但似乎不起作用,因为FetchBackend没有使用HttpClient接口
有没有办法拦截FetchBackend的Fetch请求以添加授权头?
我曾try 使用HttpInterceptor拦截这些FETCH请求,但似乎不起作用,因为FetchBackend没有使用HttpClient接口
如果你使用FetchBackend,你的提供者中可能有provideHttp(withFetch())
个.
您应该直接使用FetchBackend,而不是HttpClient(当您通过withFetch()
启用它时,它将依赖于FetchBackend.
constructor(httpClient: HttpClient) {
const url = 'http://localhost:8080/sse';
let sseRequest: HttpRequest<any> = new HttpRequest('GET', url, {
responseType: 'arraybuffer',
});
httpClient.request(sseRequest)
}
如果要设置拦截器
export const authenticationInterceptor: HttpInterceptorFn = (req: HttpRequest<unknown>, next:
HttpHandlerFn) => {
const userToken = 'MY_TOKEN'; const modifiedReq = req.clone({
headers: req.headers.set('Authorization', `Bearer ${userToken}`),
});
return next(modifiedReq);
};
它可以用作provideHttpClient(withFetch(), withInterceptors([authenticationInterceptor]))