Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make multipart mimetype configurable #3987

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading