【问题标题】:how to fetch specific value from response header in angular interceptor如何从角度拦截器中的响应标头中获取特定值
【发布时间】:2021-08-21 05:57:22
【问题描述】:

我正在尝试使用一个值来检查令牌是否已过期或未在拦截器中。 Token Expired 值出现在响应标头中。 screenshot of response header

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    
  const reqWithCredentials = req.clone({withCredentials: true});
  return next.handle(reqWithCredentials)
    .pipe(map(resp => {
      // on Response
      if (resp instanceof HttpResponse) {
        // Do whatever you want with the response.
        return resp;
      }
    }),
      catchError(error => {
        if (error instanceof HttpErrorResponse) {
          if (error.status === this._appConstant.ErrorCodes.Unauthorized || error.status === this._appConstant.ErrorCodes.Forbidden) {
            if (error.headers.has('Token-Expired')) {
              this.router.navigate(["/tokenexpired"]);
            }
            else {
              this.router.navigate(["/unauthorized"]);
            }
          }
          else {
            return throwError(error);
          }
        }
      })
    );
}

【问题讨论】:

    标签: angular http angular-http-interceptors


    【解决方案1】:
    return next.handle(reqWithCredentials)
      .pipe(map(resp => {
        // on Response
        if (resp instanceof HttpResponse) {
               if(resp.headers.get('headerName')) {
                    // Do whatever you want with the response.
                 }
          return resp;
        }
      })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-25
      • 2020-11-20
      • 2019-05-20
      • 2019-07-07
      • 2015-11-13
      • 2018-07-25
      • 2018-12-02
      • 2021-10-21
      相关资源
      最近更新 更多