Skip to content

Commit

Permalink
string parts of multipart requests are missing the content type
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Feb 18, 2024
1 parent 7d37dab commit 71061d8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ void mediaTypesForParts() {
})
.assertBodyPart("metadata", p -> {
p.assertBody("{\"foo\": 1}");
p.assertContentType("application/json");
p.assertContentDisposition("form-data; name=\"metadata\"");
});

}
Expand Down
2 changes: 2 additions & 0 deletions unirest-bdd-tests/src/test/resources/rawPost.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ Content-Type: application/octet-stream
This is a test file
--IDENTIFIER
Content-Disposition: form-data; name="funky"
Content-Type: application/x-www-form-urlencoded; charset=UTF-8

bunch
--IDENTIFIER
Content-Disposition: form-data; name="marky"
Content-Type: application/x-www-form-urlencoded; charset=UTF-8

mark
--IDENTIFIER--
11 changes: 6 additions & 5 deletions unirest/src/main/java/kong/unirest/core/java/BodyBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,31 +122,32 @@ private String toPair(ParamPart p, Body o) {


private void setMultiPart(Body o, MultipartBodyPublisher.Builder builder, BodyPart part) {
String contentType = part.getContentType();
if (part.isFile()) {
if (part instanceof FilePart) {
try {
builder.filePart(part.getName(),
((File) part.getValue()).toPath(),
part.getContentType());
contentType);
} catch (FileNotFoundException e) {
throw new UnirestException(e);
}
} else if (part instanceof InputStreamPart) {
if (part.getFileName() != null) {
builder.formPart(part.getName(), standardizeName(part, o.getMode()),
new PartPublisher(HttpRequest.BodyPublishers.ofInputStream(() -> (InputStream) part.getValue()), part.getContentType()));
new PartPublisher(HttpRequest.BodyPublishers.ofInputStream(() -> (InputStream) part.getValue()), contentType), contentType);
} else {
builder.formPart(part.getName(),
new PartPublisher(HttpRequest.BodyPublishers.ofInputStream(() -> (InputStream) part.getValue()), part.getContentType()));
new PartPublisher(HttpRequest.BodyPublishers.ofInputStream(() -> (InputStream) part.getValue()), contentType), contentType);
}

} else if (part instanceof ByteArrayPart) {
builder.formPart(part.getName(),
standardizeName(part, o.getMode()),
new PartPublisher(HttpRequest.BodyPublishers.ofByteArray((byte[]) part.getValue()), part.getContentType()));
new PartPublisher(HttpRequest.BodyPublishers.ofByteArray((byte[]) part.getValue()), contentType), contentType);
}
} else {
builder.textPart(part.getName(), String.valueOf(part.getValue()));
builder.textPart(part.getName(), String.valueOf(part.getValue()), contentType);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ private long computeLength() {

static void appendPartHeaders(StringBuilder target, Part part) {
part.headers().all().forEach(h -> appendHeader(target, h.getName(), h.getValue()));
BodyPublisher publisher = part.bodyPublisher();
if (publisher instanceof PartPublisher) {
appendHeader(target, "Content-Type", ((PartPublisher) publisher).mediaType().toString());
}
}

private static void appendHeader(StringBuilder target, String name, String value) {
Expand All @@ -138,13 +134,14 @@ static final class Builder {
/**
* Adds a form field with the given name and body.
*
* @param name the field's name
* @param name the field's name
* @param bodyPublisher the field's body publisher
* @param contentType the content type for the part
*/
MultipartBodyPublisher.Builder formPart(String name, BodyPublisher bodyPublisher) {
MultipartBodyPublisher.Builder formPart(String name, BodyPublisher bodyPublisher, String contentType) {
requireNonNull(name, "name");
requireNonNull(bodyPublisher, "body");
parts.add(new Part(name, null, bodyPublisher));
parts.add(new Part(name, null, bodyPublisher, contentType));
return this;
}

Expand All @@ -155,11 +152,11 @@ MultipartBodyPublisher.Builder formPart(String name, BodyPublisher bodyPublisher
* @param filename the field's filename
* @param body the field's body publisher
*/
MultipartBodyPublisher.Builder formPart(String name, String filename, BodyPublisher body) {
MultipartBodyPublisher.Builder formPart(String name, String filename, BodyPublisher body, String contentType) {
requireNonNull(name, "name");
requireNonNull(filename, "filename");
requireNonNull(body, "body");
parts.add(new Part(name, filename, body));
parts.add(new Part(name, filename, body, contentType));
return this;
}

Expand All @@ -170,8 +167,8 @@ MultipartBodyPublisher.Builder formPart(String name, String filename, BodyPublis
* @param name the field's name
* @param value an object whose string representation is used as the value
*/
MultipartBodyPublisher.Builder textPart(String name, Object value) {
return textPart(name, value, UTF_8);
MultipartBodyPublisher.Builder textPart(String name, Object value, String contentType) {
return textPart(name, value, UTF_8, contentType);
}

/**
Expand All @@ -182,11 +179,11 @@ MultipartBodyPublisher.Builder textPart(String name, Object value) {
* @param value an object whose string representation is used as the value
* @param charset the charset for encoding the field's body
*/
MultipartBodyPublisher.Builder textPart(String name, Object value, Charset charset) {
MultipartBodyPublisher.Builder textPart(String name, Object value, Charset charset, String contentType) {
requireNonNull(name, "name");
requireNonNull(value, "value");
requireNonNull(charset, "charset");
return formPart(name, BodyPublishers.ofString(value.toString(), charset));
return formPart(name, BodyPublishers.ofString(value.toString(), charset), contentType);
}


Expand All @@ -209,7 +206,7 @@ MultipartBodyPublisher.Builder filePart(String name, Path file, String mediaType
String filename = filenameComponent != null ? filenameComponent.toString() : "";
PartPublisher publisher =
new PartPublisher(BodyPublishers.ofFile(file), mediaType);
return formPart(name, filename, publisher);
return formPart(name, filename, publisher, mediaType);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions unirest/src/main/java/kong/unirest/core/java/Part.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ class Part {
private final String fieldName;
private final String filename;

Part(String fieldName, String filename, HttpRequest.BodyPublisher bodyPublisher) {
Part(String fieldName, String filename, HttpRequest.BodyPublisher bodyPublisher, String contentType) {
requireNonNull(bodyPublisher, "bodyPublisher");
this.fieldName = fieldName;
this.filename = filename;
this.headers = getFormHeaders(fieldName, filename);
this.headers = getFormHeaders(fieldName, filename, contentType);
this.bodyPublisher = bodyPublisher;
}

Expand All @@ -68,14 +68,15 @@ public HttpRequest.BodyPublisher bodyPublisher() {



private static Headers getFormHeaders(String name, String filename) {
private static Headers getFormHeaders(String name, String filename, String contentType) {
StringBuilder disposition = new StringBuilder();
appendEscaped(disposition.append("form-data; name="), name);
if (filename != null) {
appendEscaped(disposition.append("; filename="), filename);
}
Headers headers = new Headers();
headers.add("Content-Disposition", disposition.toString());
headers.add("Content-Type", contentType);
return headers;
}

Expand Down

0 comments on commit 71061d8

Please sign in to comment.