From 634f03a3745d237d9954d0c4e7f1d814d767afc4 Mon Sep 17 00:00:00 2001 From: Vlad-Marian MARIAN Date: Fri, 10 Jan 2025 17:48:51 +0100 Subject: [PATCH] Fix multipart file upload in HttpClient --- .../com/mimecast/robin/http/HttpClient.java | 45 ++++++++----------- .../com/mimecast/robin/http/HttpRequest.java | 2 +- 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/mimecast/robin/http/HttpClient.java b/src/main/java/com/mimecast/robin/http/HttpClient.java index 9a7ec1d6..1fff8e6a 100644 --- a/src/main/java/com/mimecast/robin/http/HttpClient.java +++ b/src/main/java/com/mimecast/robin/http/HttpClient.java @@ -106,48 +106,39 @@ private Request getRequest(HttpRequest request) { // HTTP/S POST/PUT else if (request.getMethod().equals(HttpMethod.POST) || request.getMethod().equals(HttpMethod.PUT)) { - // JSON. + RequestBody requestBody; + if (request.getContent() != null) { - if (request.getMethod().equals(HttpMethod.PUT)) { - builder.put(RequestBody.create(request.getContent().getKey(), MediaType.parse(request.getContent().getValue()))); - } else { - builder.post(RequestBody.create(request.getContent().getKey(), MediaType.parse(request.getContent().getValue()))); - } + // String content. + requestBody = RequestBody.create(request.getContent().getKey(), MediaType.parse(request.getContent().getValue())); } - - // Object else if (request.getObject() != null) { - if (request.getMethod().equals(HttpMethod.PUT)) { - builder.put(RequestBody.create(request.getObject().getKey(), MediaType.parse(request.getObject().getValue()))); - } else { - builder.post(RequestBody.create(request.getObject().getKey(), MediaType.parse(request.getObject().getValue()))); - } + // Java binary object. + requestBody = RequestBody.create(request.getObject().getKey(), MediaType.parse(request.getObject().getValue())); + } else { + // Simple form data. + requestBody = form.build(); } // Multipart request if files provided. - else if (!request.getFiles().isEmpty()) { + if (!request.getFiles().isEmpty()) { MultipartBody.Builder multipart = new MultipartBody.Builder(); multipart.setType(MultipartBody.FORM); - request.getFiles().forEach((key, value) -> multipart.addFormDataPart(key, FilenameUtils.getName(value.getValue()), + request.getFiles().forEach((key, value) -> multipart.addFormDataPart(key, FilenameUtils.getName(value.getKey()), RequestBody.create(new File(value.getKey()), MediaType.parse(value.getValue())))); request.getParams().forEach(multipart::addFormDataPart); // Add multipart data params. + multipart.addPart(requestBody); - if (request.getMethod().equals(HttpMethod.PUT)) { - builder.put(multipart.build()); - } else { - builder.post(multipart.build()); - } + requestBody = multipart.build(); } - // Simple form data. - else { - if (request.getMethod().equals(HttpMethod.PUT)) { - builder.put(form.build()); - } else { - builder.post(form.build()); - } + // Add PUT / POST to request builder. + if (request.getMethod().equals(HttpMethod.PUT)) { + builder.put(requestBody); + } else { + builder.post(requestBody); } } diff --git a/src/main/java/com/mimecast/robin/http/HttpRequest.java b/src/main/java/com/mimecast/robin/http/HttpRequest.java index 5616bbba..5f81eee7 100644 --- a/src/main/java/com/mimecast/robin/http/HttpRequest.java +++ b/src/main/java/com/mimecast/robin/http/HttpRequest.java @@ -213,6 +213,6 @@ public String toString() { new AbstractMap.SimpleEntry<>("headers", safeHeaders), new AbstractMap.SimpleEntry<>("params", params), new AbstractMap.SimpleEntry<>("files", files) - ).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))).toString(); + ).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))) + "\r\n" + content; } }