HttpClient: disabling chunked encoding
Asked Answered
G

3

7

I am using the Apache Commons HttpClient along with Restlet to call a restful web service. Unfortunately, my server (based on Ruby on Rails) does not like the Transfer-Encoding: chunked that HttpClient is using by default.

Is there any way to disable the usage of chunked encoding for POSTs from the client?

Gretagretal answered 11/10, 2011 at 4:59 Comment(0)
C
2

As said in Restlet mailing list, in Restlet version 2.1, you can set ClientResource#entityBuffering property to true to cache content in memory and prevent chunked encoding.

Canon answered 11/10, 2011 at 15:2 Comment(0)
O
6

As a general rule, for request not to be chunked, you need to specify exact size of post body, which for dynamically generated data means you need to buffer entire response in memory, see its size and only then send it.

Apache client documentation seems to confirm this: AbstractHttpEntity.setChunked() states

Note that the chunked setting is a hint only. If using HTTP/1.0, chunking is never performed. Otherwise, even if chunked is false, HttpClient must use chunk coding if the entity content length is unknown (-1).

Overanxious answered 11/10, 2011 at 10:26 Comment(0)
C
2

As said in Restlet mailing list, in Restlet version 2.1, you can set ClientResource#entityBuffering property to true to cache content in memory and prevent chunked encoding.

Canon answered 11/10, 2011 at 15:2 Comment(0)
W
2

The most reliable way, as @Slartibartfast hinted in his answer, is to explicitly switch HttpPost to HTTP 1.0 protocol.

  1. Set apache HttpPost request to HTTP 1.0 protocol (the same for HttpGet, if you need this...):

    HttpPost httpPost = new HttpPost(someUrl); httpPost.setProtocolVersion(HttpVersion.HTTP_1_0); // Since v.4.3 of Apache HttpClient

  2. When creating Multipart post request provide as an input for an attachment not an InputStream (as for HTTP 1.1, which causes chunked encoding), but an array of bytes, which you have to create from the same stream beforehand. This is why content length is known. See org.apache.http.entity.mime.MultipartEntityBuilder.addBinaryBody(String, byte[], ContentType, String)

I tested this for Android development, that required slightly different class names... (see https://github.com/andstatus/andstatus/issues/249 )

Whimsey answered 2/4, 2015 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.