From d2c59fd5e40409248b4c9eb876d67249ac881a1d Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Mon, 1 Nov 2021 09:41:58 +0100 Subject: [PATCH] HTTPCLIENT-2065: Simplify additon of content type parameters in MultipartEntityBuilder This closes #320 --- .../http/entity/mime/MultipartEntityBuilder.java | 11 +++++++++++ .../entity/mime/TestMultipartEntityBuilder.java | 14 ++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java index 543bbc717..aae926697 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java @@ -112,6 +112,17 @@ public class MultipartEntityBuilder { this.contentType = contentType; return this; } + /** + * Add parameter to the current {@link ContentType}. + * + * @param parameter The name-value pair parameter to add to the {@link ContentType}. + * @return the {@link MultipartEntityBuilder} instance. + * @since 5.2 + */ + public MultipartEntityBuilder addParameter(final BasicNameValuePair parameter) { + this.contentType = contentType.withParameters(parameter); + return this; + } public MultipartEntityBuilder setCharset(final Charset charset) { this.charset = charset; diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java index 3bbd7b7f9..9981abb77 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java @@ -120,6 +120,20 @@ public class TestMultipartEntityBuilder { entity.getContentType()); } + @Test + public void testMultipartCustomContentTypeUsingAddParameter() { + final MultipartEntityBuilder eb = MultipartEntityBuilder.create(); + eb.setMimeSubtype("related"); + eb.addParameter(new BasicNameValuePair("boundary", "yada-yada")); + eb.addParameter(new BasicNameValuePair("charset", "ascii")); + eb.addParameter(new BasicNameValuePair("my", "stuff")); + eb.buildEntity(); + final MultipartFormEntity entity = eb.buildEntity(); + Assert.assertNotNull(entity); + Assert.assertEquals("multipart/related; boundary=yada-yada; charset=US-ASCII; my=stuff", + entity.getContentType()); + } + @Test public void testMultipartWriteTo() throws Exception { final String helloWorld = "hello world";