简体   繁体   中英

Angular6 HttpInterceptor get response body

I'm trying to the get the plain text response body in an Angular6 HttpInterceptor. My code is

export class HttpErrorHandlerService implements HttpInterceptor {

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req)
      .pipe(catchError(err => {
        console.log('err body');
        console.log(err.error);
        return empty();
      }));      
  }
} 

err.error at that point is a Blob with a property type "text/plain".
I'm wanting to get the response body so I can get all error messages and put them in a toast.

It looks like Angular use to return the string instead of a Blob, but I havent found out how to do this now that its changed.

Anyone know how to get it?

Hi could you please try the following code

var reader = new FileReader();
reader.onload = function() {
    alert(reader.result);
}
reader.readAsText(err.error);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM