How do I send an HTTP response without Transfer Encoding: chunked?

Adam picture Adam · May 14, 2013 · Viewed 18.4k times · Source

I have a Java Servlet that responds to the Twilio API. It appears that Twilio does not support the chunked transfer that my responses are using. How can I avoid using Transfer-Encoding: chunked?

Here is my code:

// response is HttpServletResponse
// xml is a String with XML in it
response.getWriter().write(xml);
response.getWriter().flush();

I am using Jetty as the Servlet container.

Answer

cmbaxter picture cmbaxter · May 14, 2013

I believe that Jetty will use chunked responses when it doesn't know the response content length and/or it is using persistent connections. To avoid chunking you either need to set the response content length or to avoid persistent connections by setting "Connection":"close" header on the response.