From 71061d83c575896f4e8862ff98d182a634554248 Mon Sep 17 00:00:00 2001 From: Ryan Bergman Date: Sun, 18 Feb 2024 07:40:35 -0600 Subject: [PATCH] string parts of multipart requests are missing the content type --- .../MultiPartFormPostingTest.java | 2 ++ .../src/test/resources/rawPost.txt | 2 ++ .../kong/unirest/core/java/BodyBuilder.java | 11 ++++---- .../core/java/MultipartBodyPublisher.java | 25 ++++++++----------- .../java/kong/unirest/core/java/Part.java | 7 +++--- 5 files changed, 25 insertions(+), 22 deletions(-) diff --git a/unirest-bdd-tests/src/test/java/BehaviorTests/MultiPartFormPostingTest.java b/unirest-bdd-tests/src/test/java/BehaviorTests/MultiPartFormPostingTest.java index fce25f39b..cad9956ee 100644 --- a/unirest-bdd-tests/src/test/java/BehaviorTests/MultiPartFormPostingTest.java +++ b/unirest-bdd-tests/src/test/java/BehaviorTests/MultiPartFormPostingTest.java @@ -441,6 +441,8 @@ void mediaTypesForParts() { }) .assertBodyPart("metadata", p -> { p.assertBody("{\"foo\": 1}"); + p.assertContentType("application/json"); + p.assertContentDisposition("form-data; name=\"metadata\""); }); } diff --git a/unirest-bdd-tests/src/test/resources/rawPost.txt b/unirest-bdd-tests/src/test/resources/rawPost.txt index 5300d6623..83945aa42 100644 --- a/unirest-bdd-tests/src/test/resources/rawPost.txt +++ b/unirest-bdd-tests/src/test/resources/rawPost.txt @@ -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-- \ No newline at end of file diff --git a/unirest/src/main/java/kong/unirest/core/java/BodyBuilder.java b/unirest/src/main/java/kong/unirest/core/java/BodyBuilder.java index fc5224ec0..6965e5174 100644 --- a/unirest/src/main/java/kong/unirest/core/java/BodyBuilder.java +++ b/unirest/src/main/java/kong/unirest/core/java/BodyBuilder.java @@ -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); } } diff --git a/unirest/src/main/java/kong/unirest/core/java/MultipartBodyPublisher.java b/unirest/src/main/java/kong/unirest/core/java/MultipartBodyPublisher.java index 3a294982a..9f9b5412b 100644 --- a/unirest/src/main/java/kong/unirest/core/java/MultipartBodyPublisher.java +++ b/unirest/src/main/java/kong/unirest/core/java/MultipartBodyPublisher.java @@ -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) { @@ -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; } @@ -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; } @@ -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); } /** @@ -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); } @@ -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); } /** diff --git a/unirest/src/main/java/kong/unirest/core/java/Part.java b/unirest/src/main/java/kong/unirest/core/java/Part.java index 7293ac185..0f845694c 100644 --- a/unirest/src/main/java/kong/unirest/core/java/Part.java +++ b/unirest/src/main/java/kong/unirest/core/java/Part.java @@ -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; } @@ -68,7 +68,7 @@ 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) { @@ -76,6 +76,7 @@ private static Headers getFormHeaders(String name, String filename) { } Headers headers = new Headers(); headers.add("Content-Disposition", disposition.toString()); + headers.add("Content-Type", contentType); return headers; }