如何“禁用”特定呼叫的拦截

时间:2020-01-22 03:06:38

标签: angular httpservice

我有一个大应用程序,到目前为止,它只使用一个http服务器来发出请求。

在app.module中,我具有以下拦截器:

  providers: [
    {
      provide: LocationStrategy,
      useClass: HashLocationStrategy
    },
    {
      provide: HTTP_INTERCEPTORS,
      useClass: JwtInterceptor,
      multi: true
    },
    {
      provide: HTTP_INTERCEPTORS,
      useClass: ErrorInterceptor,
      multi: true
    }],

问题是,我需要从外部服务向端点添加一个API调用,并且希望仅针对此API调用才能抵制此拦截器。

getAllCesiumModels(params?: apiModels.GetAllCesiumRequest): Observable<apiModels.GetAllModelsResponse>  {
  return this.httpClient.get<any>(`https://api.cesium.com/v1/assets`, {headers: { Authorization: `Bearer ${params.accessToken}` }});
}

有没有一种方法可以在不更改所有应用程序的情况下发出此获取请求,而不跳过拦截器?

谢谢

2 个答案:

答案 0 :(得分:1)

不确定是否可行,但是您可以试一下。

export class JwtInterceptor implements HttpInterceptor {
  public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
     return next.handle(request).pipe(
            catchError((err) => {
                if(request.url.indexOf("your web app url")==-1)
                {
                  return EMPTY;
                }
            })
        )
   }
}

答案 1 :(得分:0)

服务舱

bypassedClient: HttpClient

constructor(
   private handler: HttpBackend
) {
   this.bypassedClient = new HttpClient(handler);
}

makeCall(){
this.bypassedClient.post(this.apiURL + Constant.API.authenticate, params)
.subscribe((res)=> console.log(res))
}