How use async service into angular httpClient interceptor

前端 未结 7 921
轻奢々
轻奢々 2020-12-09 18:53

Using Angular 4.3.1 and HttpClient, I need to modify the request and response by async service into the HttpInterceptor of httpClient,

Example for modifying the requ

7条回答
  •  萌比男神i
    2020-12-09 19:24

    I think that there is a issue about the reactive flow. The method intercept expects to return an Observable and you have to flatten your async result with the Observable returned by next.handle

    Try this

    intercept(req: HttpRequest, next: HttpHandler): Observable> {
          return this.asyncService.applyLogic(req).mergeMap((modifiedReq)=> {
            const newReq = req.clone(modifiedReq);
            return next.handle(newReq);
        });
    }
    

    You could also use switchMap instead of mergeMap

提交回复
热议问题