I am working on an Angular 4 application using Spring Boot. Basically right now I have a service that upload a file from my Angular app to my Spring Boot server.
I want to print in the Angular logs the message from the ResponseEntity
object that I receive from Spring, but I can't figure how to use the event object to get the message, here's my code sample:
EDIT1: console.log(event.body)
prints well message1
or message2
, but I need to have event.body
as a String to use it in a specific function.
Right now it is HttpResponse<{}>.body: {}
but I don't know what it means in TS and how could I transform it to a String.
EDIT2: I think it should be like that
if(event.body instanceof String) function(event.body);
But I still get a type conflict between String and string. And modifying it to string
, it says that 'string' is use as a value here.
UploadService:
uploadFile(file: File): Observable<HttpEvent<{}>> {
let formdata: FormData = new FormData();
formdata.append('file', file);
const req = new HttpRequest('POST', '/post', formdata, {
reportProgress: true,
responseType: 'text'
});
return this.http.request(req);
}
Upload method:
this.uploadService.uploadFile(file).subscribe(event => {
if (event.type === HttpEventType.UploadProgress) {
//Do some stuff
} else if (event instanceof HttpResponse) {
//message = event.body ?? //How to get message 1 or 2 below???
console.log(message)
});
console.log('File ' + file.name + ' is completely uploaded!');
}
});
Here's the response from my Spring Boot server:
return ResponseEntity.status(HttpStatus.OK).body("message1");
or:
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body("message2");