Reputation: 89
I'm using angular 9 and interceptors ... The API that I'm using encrypts the JSON response in AES, and when the API responses then I need to decrypt the response in angular 9 ... I need to get the raw response but I don't see the way to do that ... Interceptors in angular 9 looks that does not allow me to do that. What I have done is:
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map, tap, catchError, filter, scan } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class ResponseInterceptor implements HttpInterceptor {
constructor() {}
intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
return next.handle(req).pipe(
tap(evt => {
if (evt instanceof HttpResponse) {
console.log(evt);
}
}));
}
}
The API response is:
kVwKXPYoRTPNZVv0X6pCWA9cpj7CqGV1h4zvI8X4F47FIJpTwLAP497JquiC+t95skz9HSdUJfizjPz+7jokcxUuW1dUdP8/MTp+Gn0TxPHTF+o6gUFj3sZJQGESMolINa0vjuvaHogiIcdn0XO6mdeDgD6AIOqqgvOUM2EYwcjzHXC3Ag8wL0ybeSMZB7j3XFmcfO97EohLM2yPBvwqeO0a8Dqzsn+DE5VXNOdcK8ArzXbXe8eARQl+vajGU6JuFsSsSU+cNil2AQHemyzknl02MWL7nIAps4SgynpGRw4z0IIhE2EbJANj5WzoxvOHNbGYPjkdPezO91CKeWsdZdCcDcJeJqdclvuntXRnkZVHTB/zARgiYLREHQ/eO5/5W25JR3En3+B3yIyoXI+W70fgLOZofSPYK6yUrkO0nZoAEdFGPcvOVx9eaQraDjGLG7JrnpRpXqscW8sFpaNvR2Cfay8gRJcscGDZlsSlhNMce6r/2WwDhQx29Dkvt2V+dBb4obrHTptqdCcq+uKnir/2cTUIOm03anZWU2c8u3WFxAalYbISGZrH6HySE/4ncP5y7Cm5ziAxDFLP4c4La1yKMKsFmBEfmzzdhgq8gGE6M3UPpLdaCF2yc4ddpJ7+l5QBT2pXIKQcNUraGbyD35hoB5AKyLfxTGd8wW2qVGe4RrrUXs5GhrZRZk0BwEFYG/kpQmGUvJ0hCA6lzUypOnYzNOeJ5s4XmprFhI9qIZPUoC/JroCnlw/+O4MN9cYI96RMvBICoL4+01c+y+ykppFP48kmKgk4+42ZFFC8mHg55rq9hTjJk5TKzq/N7Np2
And when execute the response the evt
variable type is type
and the value is 0. Any suggestion ???
The interceptor does not allow me to apply the encryption to the response and suddenly just try to convert it to JSON.
Upvotes: 2
Views: 1922
Reputation: 89
Ok, while I get a more efficient and expert answer ... I solved like this:
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
catchError(err => {
this.alertService.clear();
if (err.status === 200){
return of(new HttpResponse({ status: 200, body: JSON.parse(aes.decrypt(err.error.text)) }));
}
if (err.status === 401) {
this.authenticationService.logout();
location.reload(true);
}
const error = err.error.message || err.statusText;
this.alertService.error(error);
console.log(err);
}))
}
Upvotes: 4