Skip to content

Commit

Permalink
Support configuring multipart mimetype (#3987)
Browse files Browse the repository at this point in the history
  • Loading branch information
roumn authored Nov 14, 2024
1 parent 0fd2de6 commit c6169a8
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,20 @@ public class MultiValuePart {
protected final String name;
protected final Object body;
protected final String filename;
protected final String mimeType;

protected MultiValuePart(String name, Object body, String filename) {
this.name = name;
this.body = body;
this.filename = filename;
this.mimeType = null;
}

protected MultiValuePart(String name, Object body, String filename, String mimeType) {
this.name = name;
this.body = body;
this.filename = filename;
this.mimeType = mimeType;
}

public String getName() {
Expand All @@ -39,11 +48,19 @@ public String getFilename() {
return filename;
}

public String getMimeType() {
return mimeType;
}

public static MultiValuePart fromText(String name, String value) {
return new MultiValuePart(name, value, null);
}

public static MultiValuePart fromFile(String name, byte[] value, String filename) {
return new MultiValuePart(name, value, filename);
}

public static MultiValuePart fromFile(String name, byte[] value, String filename, String mimeType) {
return new MultiValuePart(name, value, filename, mimeType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,11 @@ protected void setRequestEntity(HttpRequest requestInfo, HttpEntityEnclosingRequ
String name = part.getName();
Object value = part.getBody();
if (value instanceof byte[]) {
entityBuilder.addBinaryBody(name, (byte[]) value, ContentType.DEFAULT_BINARY, part.getFilename());
if (StringUtils.isNotBlank(part.getMimeType())) {
entityBuilder.addBinaryBody(name, (byte[]) value, ContentType.create(part.getMimeType()), part.getFilename());
} else {
entityBuilder.addBinaryBody(name, (byte[]) value, ContentType.DEFAULT_BINARY, part.getFilename());
}
} else if (value instanceof String) {
entityBuilder.addTextBody(name, (String) value);
} else if (value != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,11 @@ protected void setRequestEntity(HttpRequest requestInfo, AsyncRequestBuilder req
String name = part.getName();
Object value = part.getBody();
if (value instanceof byte[]) {
entityBuilder.addBinaryBody(name, (byte[]) value, ContentType.DEFAULT_BINARY, part.getFilename());
if (StringUtils.isNotBlank(part.getMimeType())) {
entityBuilder.addBinaryBody(name, (byte[]) value, ContentType.create(part.getMimeType()), part.getFilename());
} else {
entityBuilder.addBinaryBody(name, (byte[]) value, ContentType.DEFAULT_BINARY, part.getFilename());
}
} else if (value instanceof String) {
entityBuilder.addTextBody(name, (String) value);
} else if (value != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,12 @@ protected void setRequestEntity(HttpRequest requestInfo, WebClient.RequestBodySp
if (value instanceof byte[]) {
value = new ByteArrayResourceWithFileName((byte[]) value, part.getFilename());
}

MultipartBodyBuilder.PartBuilder partBuilder = multipartBodyBuilder.part(name, value);
MultipartBodyBuilder.PartBuilder partBuilder;
if (StringUtils.isNotBlank(part.getMimeType())) {
partBuilder = multipartBodyBuilder.part(name, value, MediaType.parseMediaType(part.getMimeType()));
} else {
partBuilder = multipartBodyBuilder.part(name, value);
}
if (StringUtils.isNotEmpty(part.getFilename())) {
partBuilder.filename(part.getFilename());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,52 @@ void postWithMultiPart(FlowableHttpClient httpClient) {
.containsExactly("application/json");
}

@ParameterizedTest
@ArgumentsSource(FlowableHttpClientArgumentProvider.class)
void postWithMultiPartWithMimeType(FlowableHttpClient httpClient) {
HttpRequest request = new HttpRequest();
request.setUrl("http://localhost:9798/api/test-multi?testArg=testMultiPartValueWithMimeType");
request.setMethod("POST");
request.addMultiValuePart(MultiValuePart.fromFile("document", "kermit;gonzo".getBytes(StandardCharsets.UTF_8), "kermit.csv", "text/csv"));
request.addMultiValuePart(MultiValuePart.fromFile("myJson", "{'value':'kermit'}".getBytes(StandardCharsets.UTF_8), "kermit.json", "application/json"));

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("X-Test", "Test MultiPart Value With MimeType");
request.setHttpHeaders(httpHeaders);
HttpResponse response = httpClient.prepareRequest(request).call();

assertThatJson(response.getBody())
.when(Option.IGNORING_EXTRA_FIELDS)
.isEqualTo("{"
+ " url: 'http://localhost:9798/api/test-multi',"
+ " args: {"
+ " testArg: [ 'testMultiPartValueWithMimeType' ]"
+ " },"
+ " headers: {"
+ " X-Test: [ 'Test MultiPart Value With MimeType' ]"
+ " },"
+ " parts: {"
+ " myJson: ["
+ " {"
+ " content: \"{'value':'kermit'}\","
+ " filename: 'kermit.json',"
+ " contentType: 'application/json'"
+ " }"
+ " ],"
+ " document: ["
+ " {"
+ " content: 'kermit;gonzo',"
+ " filename: 'kermit.csv',"
+ " contentType: 'text/csv'"
+ " }"
+ " ]"
+ " }"
+ "}");
assertThat(response.getStatusCode()).isEqualTo(200);
assertThat(response.getHttpHeaders().get("Content-Type"))
.containsExactly("application/json");
}

@ParameterizedTest
@ArgumentsSource(FlowableHttpClientArgumentProvider.class)
void deleteWithoutBody(FlowableHttpClient httpClient) {
Expand Down

0 comments on commit c6169a8

Please sign in to comment.