0

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.

In Postman enter image description here

8
  • 1
    please show what content type you are using in your headers in postman Commented Apr 23, 2020 at 14:25
  • This is it. multipart/form-data; boundary=<calculated when request is sent> Commented Apr 23, 2020 at 14:32
  • As @emmanuelagarry suggests you, maybe the error is in the content-type of your header. Commented Apr 23, 2020 at 14:33
  • If I set tomultipart/form-data, I receive error Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found Commented Apr 23, 2020 at 14:38
  • See stackoverflow.com/questions/36005436/… Commented Apr 23, 2020 at 14:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.