Ami*_*abh 21 interceptor typescript angular
我正在使用Angular 4.3.1和HttpClient.有一个HttpInterceptor来设置一些标题.
在一些http get请求中,我需要设置一个不同的头.无论如何我可以为这个特定的HttpRequest传递一些param给这个HttpInterceptor?
@Injectable()
export class MyHttpInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if(request.custom.param1) // how can i do this
request = request.clone({
setHeaders: {
'header1': 'xxxxxx'
}
});
else
request = request.clone({
setHeaders: {
'header2': 'yyyyyy'
}
});
return next.handle(request);
}
}
Run Code Online (Sandbox Code Playgroud)
JWe*_*ess 13
我写了一个用于处理Http错误响应的拦截器.我想允许特定的Http调用来指示拦截器忽略某些响应状态代码,同时还保留将params传递给Http调用的能力.这是我最终得到的解决方案.(谢谢,Aleksey在你的回答中提出了最初的想法).
扩展HttpParams:
import { HttpParams } from '@angular/common/http';
import { HttpParamsOptions } from '@angular/common/http/src/params';
// Cause the HttpErrorInterceptor to ignore certain error response status codes like this:
//
// this.http.get<TypeHere>(`URL_HERE`, {
// params: new InterceptorHttpParams({ statusCodesToIgnore: [400, 401] }, {
// complete: 'false',
// offset: '0',
// limit: '50'
// })
// })
export class InterceptorHttpParams extends HttpParams {
constructor(
public interceptorConfig: { statusCodesToIgnore: number[] },
params?: { [param: string]: string | string[] }
) {
super({ fromObject: params } as HttpParamsOptions);
}
}
Run Code Online (Sandbox Code Playgroud)
拦截器:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).pipe(
tap(
() => {},
(error: any) => {
if (error instanceof HttpErrorResponse) {
const regEx = /^[4-5][0-9][0-9]$/; // 4XX and 5XX status codes
if (regEx.test(error.status.toString())) {
const errorMessage = this.getErrorMessageFromStatus(error.status);
if (!this._shouldIgnoreError(req, error)) {
console.log(`ERROR INTERCEPTOR: ${error.status}`);
this.toastService.alert(errorMessage);
}
}
}
})
);
}
// Based on `request.params.interceptorConfig.statusCodesToIgnore`, we can see if we should ignore this error.
_shouldIgnoreError(request: HttpRequest<any>, errorResponse: HttpErrorResponse) {
if (request.params instanceof InterceptorHttpParams
&& Array.isArray(request.params.interceptorConfig.statusCodesToIgnore)
&& request.params.interceptorConfig.statusCodesToIgnore.includes(errorResponse.status)) {
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
Ale*_* L. 12
也许有更好的方法来处理这个问题,但作为一种解决方法,您可以创建并传递自定义HttpParams请求,然后在拦截器中检查它们.例如:
export class CustomHttpParams extends HttpParams {
constructor(public param1: boolean) {
super();
}
}
Run Code Online (Sandbox Code Playgroud)
在http调用中使用此类:
this.http.get('https://example.com', {
params: new CustomHttpParams(true)
})
Run Code Online (Sandbox Code Playgroud)
现在在拦截器中:
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (request.params instanceof CustomHttpParams && request.params.param1)
request = request.clone({
setHeaders: {
'header1': 'xxxxxx'
}
});
else
request = request.clone({
setHeaders: {
'header2': 'yyyyyy'
}
});
return next.handle(request);
}
Run Code Online (Sandbox Code Playgroud)
在 Angular 12+ 中,有一个新属性HttpRequest.context旨在解决这个问题。
拦截器可以使用的共享和可变上下文 https://angular.io/api/common/http/HttpRequest#context
| 归档时间: |
|
| 查看次数: |
8903 次 |
| 最近记录: |