diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/ByteArrayBody.java b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/ByteArrayBody.java index 3fdb87141..fba5effc9 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/ByteArrayBody.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/ByteArrayBody.java @@ -56,11 +56,22 @@ public class ByteArrayBody extends AbstractContentBody { */ public ByteArrayBody(final byte[] data, final ContentType contentType, final String filename) { super(contentType); - Args.notNull(data, "byte[]"); - this.data = data; + this.data = Args.notNull(data, "data"); this.filename = filename; } + /** + * Public constructor that creates a new ByteArrayBody. + * + * @param contentType the {@link ContentType} + * @param data the array of byte. + * + * @since 5.2 + */ + public ByteArrayBody(final byte[] data, final ContentType contentType) { + this(data, contentType, null); + } + /** * Creates a new ByteArrayBody. * 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 5e55f40b5..543bbc717 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 @@ -155,7 +155,7 @@ public class MultipartEntityBuilder { public MultipartEntityBuilder addBinaryBody( final String name, final byte[] b) { - return addBinaryBody(name, b, ContentType.DEFAULT_BINARY, null); + return addPart(name, new ByteArrayBody(b, ContentType.DEFAULT_BINARY)); } public MultipartEntityBuilder addBinaryBody( 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 abaedb28e..3bbd7b7f9 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 @@ -70,13 +70,15 @@ public class TestMultipartEntityBuilder { .addBinaryBody("p2", new File("stuff")) .addBinaryBody("p3", new byte[]{}) .addBinaryBody("p4", new ByteArrayInputStream(new byte[]{})) + .addBinaryBody("p5", new ByteArrayInputStream(new byte[]{}), ContentType.DEFAULT_BINARY, "filename") .buildEntity(); Assert.assertNotNull(entity); final List bodyParts = entity.getMultipart().getParts(); Assert.assertNotNull(bodyParts); - Assert.assertEquals(4, bodyParts.size()); + Assert.assertEquals(5, bodyParts.size()); } + @Test public void testMultipartCustomContentType() throws Exception { final MultipartFormEntity entity = MultipartEntityBuilder.create()