upload(@RequestPart("det" /> upload(@RequestPart("det" /> upload(@RequestPart("det"/>

Using apache http client to upload file using multipart to spring boot

49 views Asked by At

I have an spring boot application that has end point like this

@PostMapping("/")
public ResponseEntity<FileUploadResponseDto> upload(@RequestPart("details") FileUploadDto fileUploadDto,@RequestParam("file") MultipartFile file){
  try {
      return ResponseEntity.ok( fileUploadService.uploadFile(fileUploadDto,file));
  }catch (Exception e){
      return ResponseEntity.status(406).body(new FileUploadResponseDto(false,e.getMessage()));
  }
};

When using postman everything works fine. This is postman example enter image description here

But when i try to use http apache client the spring boot returns application/octet-stream is not supported

enter image description here

What Am I doing wrong here ?

EDIT

code apache client

String json = mapToJson(Map.of("name",(String)file[1],"lastname",    

(String)file[2],"createTime",(String)file[3]));
       final MultipartEntityBuilder builder  =                               
       MultipartEntityBuilder.create();

       builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
       builder.addTextBody("details",json,ContentType.APPLICATION_JSON);
       builder.addBinaryBody("file",(File)file[4],ContentType.APPLICATION_OCTET_STREAM,((File)file[4]).getName());

       final HttpPost httpPost = new HttpPost("http://"+host+":"+port+"/api/v1/upload/");
       try(CloseableHttpClient client = HttpClientBuilder.create().build()) {

           final HttpEntity entity = builder.build();
           httpPost.setEntity(entity);
           HttpResponse httpResponse = client.execute(httpPost);

           System.out.println(httpResponse);

       } catch (IOException e) {
           throw new RuntimeException(e);
       }

This is the message/response I get

HttpResponseProxy{HTTP/1.1 415 [Accept: application/json, application/*+json, Content-Type: application/json, Transfer-Encoding: chunked, Date: Sun, 11 Feb 2024 22:02:27 GMT, Keep-Alive: timeout=60, Connection: keep-alive] ResponseEntityProxy{[Content-Type: application/json,Chunked: true]}}

SOLUTION:

I am not sure why is this happening, but I had this mode configured builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

and when I debugged inside spring, request would come little bit messed up, for example 'details' part would not even have content-type it would be null.

then i switched the mode to builder.setMode(HttpMultipartMode.STRICT); and then in request.getParts(), 'details' would have content-type application json

1

There are 1 answers

0
Filip Cacic On

I am not sure why is this happening, but I had this mode configured builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

and when I debugged inside spring, request would come little bit messed up, for example 'details' part would not even have content-type it would be null.

then i switched the mode to builder.setMode(HttpMultipartMode.STRICT); and then in request.getParts(), 'details' would have content-type application json