I am trying to send a multipart request through apache async http client, but getting a org.apache.http.ContentTooLongException: Content length is unknown
error.
I do understand why content length is required in an asynchronous model, but want to understand whether this is done by design or its a bug?
I would expect it to load the stream into memory buffer and use it (this is what AsyncHttpClient does).
My test use-case is:
public class InputStreamAsMultipartPayloadTest {
@Rule
public WireMockClassRule instanceRule = new WireMockClassRule(9090);
@Test
public void inputStreamPartTest() throws Exception {
String partBody = "hello";
instanceRule.stubFor(any(urlPathEqualTo("/hello"))
.withMultipartRequestBody(
aMultipart()
.withName("part-name")
.withHeader("Content-Type", equalTo("application/octet-stream"))
.withHeader("Content-Transfer-Encoding", equalTo("binary"))
.withBody(equalTo(partBody)))
.willReturn(aResponse()));
byte[] body = partBody.getBytes(UTF_8);
try (CloseableHttpAsyncClient httpAsyncClient = HttpAsyncClients.createDefault()) {
httpAsyncClient.start();
HttpPost postRequest = new HttpPost(URI.create("/hello"));
MultipartEntityBuilder multipartBuilder = MultipartEntityBuilder.create();
multipartBuilder.addBinaryBody("part-name", new ByteArrayInputStream(body));
postRequest.setEntity(multipartBuilder.build());
Future<HttpResponse> fHttpResponse = httpAsyncClient.execute(HttpHost.create("http://localhost:9090"), postRequest, null);
assertThat(fHttpResponse.get(3, TimeUnit.SECONDS).getStatusLine().getStatusCode()).isEqualTo(HttpURLConnection.HTTP_OK);
}
}
}
The exception stack trace is:
Caused by: org.apache.http.ContentTooLongException: Content length is unknown
at org.apache.http.entity.mime.MultipartFormEntity.getContent(MultipartFormEntity.java:101)
at org.apache.http.nio.entity.EntityAsyncContentProducer.produceContent(EntityAsyncContentProducer.java:65)
at org.apache.http.nio.protocol.BasicAsyncRequestProducer.produceContent(BasicAsyncRequestProducer.java:125)
at org.apache.http.impl.nio.client.MainClientExec.produceContent(MainClientExec.java:262)
at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.produceContent(DefaultClientExchangeHandlerImpl.java:140)
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.outputReady(HttpAsyncRequestExecutor.java:241)
at org.apache.http.impl.nio.DefaultNHttpClientConnection.produceOutput(DefaultNHttpClientConnection.java:290)
at org.apache.http.impl.nio.client.InternalIODispatch.onOutputReady(InternalIODispatch.java:86)
at org.apache.http.impl.nio.client.InternalIODispatch.onOutputReady(InternalIODispatch.java:39)
at org.apache.http.impl.nio.reactor.AbstractIODispatch.outputReady(AbstractIODispatch.java:145)
at org.apache.http.impl.nio.reactor.BaseIOReactor.writable(BaseIOReactor.java:188)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:341)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:588)
The complete source code can be found at: https://github.com/maximkir/apache-async-http
In general, I am the author of SHCF4J (Simple HTTP Client For Java), which strives to provide a unified facade for common HTTP client use cases. I can fix it on the facade level but prefer to keep its logic as minimal as possible.