'Trying to send a image as byte[] from Spring backend to Angular frontend but:

The backend code:

@GetMapping("datamatrix")
public ResponseEntity<byte[]> generarDataMatrix(Long id) throws IOException {

    byte[] dataMatrixEnBytes = dataMatrix.generarDataMatrixImagen(id.toString(), id);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.IMAGE_JPEG);
    return new ResponseEntity<>(dataMatrixEnBytes, headers, HttpStatus.OK);
}

In Postman the response is OK:

enter image description here

But in Angular...

enter image description here

Angular Code:

  • Service:
generarDataMatrix( articulo: Articulo ): Observable<any> {
  return this.httpClient.get<any>( this.rootUrl + "datamatrix?id=" + articulo.id );
}
  • Method caller:
generarDataMatrix() {
  this.dbServiceArticulo.generarDataMatrix( this.articuloSeleccionado )
    .subscribe( (res) => {console.log( "res" )});
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source