I have an http call from a backend service that works correctly, this backend service also returns a ResponseEntity<String>(HttpStatus.OK)
or against a ResponseEntity<String>(HttpStatus.NOT_FOUND)
, but I would like to print the response status in the console, can you tell me the correct syntax? I paste my code: Service:
borrarInforme(): Observable<HttpResponse<string>> {
return this.httpClient.post<any>(`${this.baseURL + '/borrarInforme'}`, '').pipe(
catchError(this.handleError)
);
}
guardarInforme(): Observable<HttpResponse<string>> {
console.log("entro en el metodo para guardar informe");
return this.httpClient.post<any>(`${this.baseURL + '/guardarInforme'}`, '').pipe(
catchError(this.handleError)
);
}
handleError(error: HttpErrorResponse) {
alert('Ha ocurrido un error abre inspeccion para comprobarlo')
if (error.status === 0) {
console.error(`Ha ocurrido un error: ${error.error}`)
} else {
console.error(`error en el backend ${error.status}. el error de la respuesta es: ${error.error}`);
}
return throwError(
() => 'Algo no ha salido bien'
);
}
Next, the component that subscribes to the response of the call, as you can see, I have tried to indicate the type of the response but it tells me that it is null (response prints null) so I understand that the problem is in the service not being correctly structured the method:
confirmarOk() {
this.servicio.guardarInforme().subscribe(
(response: HttpResponse<string>) => console.log(response.status)
);
this.dialogRef.close();
this.router.navigate(['/home']);
}
cancelar() {
this.servicio.borrarInforme().subscribe(
response => console.log(response)
);
this.dialogRef.close();
}
Next I paste the controller method:
@PostMapping(/guardarInforme)
public ResponseEntity<String> guardarInforme(){
if(this.servicioReportingApp.guardarPDF()){
return new ResponseEntity<String>(HttpStatus.OK);
}
return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
}
@PostMapping(/borrarInforme)
public ResponseEntity<String> borrarInforme(){
if(this.servicioReportingApp.borrarPDF()){
return new ResponseEntity<String>(HttpStatus.OK);
}
return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
}