Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer

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).

share|improve this answer

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 )

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.