You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Karthik R <ra...@zoho.com> on 2016/01/12 13:49:28 UTC
Upgrade from 3.1 to 4.5 - Multipart form entity related exception
Hi,
We want to use the HttpAsyncClient and NIO so that we can handle more requests with fewer threads.
I am tasked with migrating from HttpComponents 3.1 to 4.5. The new API is very different from the earlier one and I need some help with the same.
ByteArrayPartSource horA = new ByteArrayPartSource("data", data.toString().getBytes("UTF-8"));
FilePart p1 = new FilePart("data", horA);
StringPart encodingPart = new StringPart("encoded", "" + _encode);
StringPart contentPart = new StringPart("content-type", "text/xml");
Part[] partes = { p1, encodingPart, contentPart };
post.setRequestEntity(new MultipartRequestEntity(partes, post.getParams()));
I rewrote the whole thing above as:
HttpEntity postEntity = MultipartEntityBuilder.create()
.addTextBody("content-type", contentType)
.addTextBody("encoded", ""+true)
.addBinaryBody("data", data.toString().getBytes("UTF-8"))
.build();
post.setEntity(postEntity);
This results in UnsupportedOperationException:Multipart form entity does not implement #getContent()
I read similar posts on this list but didn't understand what I should do. I don't want to write copy content into memory/buffer. What is the right way to write this using 4.5 and HttpAsyncClient? Any pointers will be really appreciated.
Thanks!
Re: Upgrade from 3.1 to 4.5 - Multipart form entity related
exception
Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2016-01-13 at 00:33 -0800, Karthik R wrote:
>
> Hi all,
>
>
>
> Just wanted to post a quick update. I have changed it as below and it works:
>
>
>
> NByteArrayEntity nbe = new NByteArrayEntity(data.toString().getBytes("UTF-8"));
>
> post.setEntity(nbe);
>
> post.addHeader(HttpHeaders.AUTHORIZATION,"Basic "+authStr);
>
> post.addHeader(HttpHeaders.CONTENT_TYPE,contentType);
>
> I stopped using MultipartEntityBuilder. It doesn't really give NIO support I think because it needs to be wrapped in a BufferedHttpEntity (this makes a copy of the content in memory). I am not sure if the above reasoning is correct or complete. Perhaps some one can clarify.
>
>
The multipart entity implementation in HttpClient is inherently
synchronous (blocking). It cannot be used with the async version of HC
without buffering. However, one can develop a custom
HttpAsyncContentProducer that streams out parts of multipart entity
without blocking.
Oleg
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org
Re: Upgrade from 3.1 to 4.5 - Multipart form entity related
exception
Posted by Karthik R <ra...@zoho.com>.
Hi all,
Just wanted to post a quick update. I have changed it as below and it works:
NByteArrayEntity nbe = new NByteArrayEntity(data.toString().getBytes("UTF-8"));
post.setEntity(nbe);
post.addHeader(HttpHeaders.AUTHORIZATION,"Basic "+authStr);
post.addHeader(HttpHeaders.CONTENT_TYPE,contentType);
I stopped using MultipartEntityBuilder. It doesn't really give NIO support I think because it needs to be wrapped in a BufferedHttpEntity (this makes a copy of the content in memory). I am not sure if the above reasoning is correct or complete. Perhaps some one can clarify.
Thanks again!
---- On Tue, 12 Jan 2016 04:49:28 -0800 Karthik R <rabbitcodes@zoho.com>wrote ----
Hi,
We want to use the HttpAsyncClient and NIO so that we can handle more requests with fewer threads.
I am tasked with migrating from HttpComponents 3.1 to 4.5. The new API is very different from the earlier one and I need some help with the same.
ByteArrayPartSource horA = new ByteArrayPartSource("data", data.toString().getBytes("UTF-8"));
FilePart p1 = new FilePart("data", horA);
StringPart encodingPart = new StringPart("encoded", "" + _encode);
StringPart contentPart = new StringPart("content-type", "text/xml");
Part[] partes = { p1, encodingPart, contentPart };
post.setRequestEntity(new MultipartRequestEntity(partes, post.getParams()));
I rewrote the whole thing above as:
HttpEntity postEntity = MultipartEntityBuilder.create()
.addTextBody("content-type", contentType)
.addTextBody("encoded", ""+true)
.addBinaryBody("data", data.toString().getBytes("UTF-8"))
.build();
post.setEntity(postEntity);
This results in UnsupportedOperationException:Multipart form entity does not implement #getContent()
I read similar posts on this list but didn't understand what I should do. I don't want to write copy content into memory/buffer. What is the right way to write this using 4.5 and HttpAsyncClient? Any pointers will be really appreciated.
Thanks!