Skip to content

Commit

Permalink
Fix multipart file upload in HttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarian2 committed Jan 10, 2025
1 parent 9c3fc01 commit 634f03a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
45 changes: 18 additions & 27 deletions src/main/java/com/mimecast/robin/http/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/mimecast/robin/http/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 634f03a

Please sign in to comment.