I am using Angular 8 and spring boot. If I upload image and object from Postman, it successfully uploads. If I try the same from Angular it fails and error is
[org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported]
If I set headers to following in Angular
var options = { headers: new HttpHeaders().set('Content-Type', 'application/json;charset=UTF-8') };
return this.http.patch(`${environment.apiUrl}products/`, data, options);
I still get the error
[org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported]
Here is the controller in Spring Boot
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@PreAuthorize("hasRole('USER')")
public ResponseEntity<?> create(
@RequestPart(value = "file", required = false) MultipartFile file,
@RequestPart(value = "product") @Valid ProductDTO productDTO) throws IOException
In Angular this is file upload input
<input type="file" id="file" (change)="handleFileInput($event.target.files)" accept="image/*">
In component
fileToUpload: File = null;
This is file upload function
handleFileInput(files: FileList) {
this.fileToUpload = files.item(0);
}
In Submit method
const formData = new FormData();
formData.append('file', this.fileToUpload);
formData.append('product', this.form.value);
This is all happening in Angular and Spring, but in Postman it is successfully updating.