How to send an HTTP response without Transfer Encoding: chunked?

I have a Java servlet that responds to the Twilio API. It seems that Twilio does not support the broadcast I sent, which my answers use. 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 use Jetty as a Servlet container.

+4
source share
3 answers

Before writing to the stream, try setting Content-length . Remember to calculate the number of bytes according to the correct encoding, for example:

 final byte[] content = xml.getBytes("UTF-8"); response.setContentLength(content.length); response.setContentType("text/xml"); // or "text/xml; charset=UTF-8" response.setCharacterEncoding("UTF-8"); final OutputStream out = response.getOutputStream(); out.write(content); 
+3
source

I believe that Jetty will use chunked responses when it does not know the length of the contents of the response and / or uses persistent connections. To avoid fragmentation, you need to either set the length of the content of the response or avoid constant connections by setting the message “Connection”: “close” in the response.

+7
source

The container itself decides to use Content-Length or Transfer-Encoding based on the size of the data to be written using Writer or outputStream . If the data size is larger than HttpServletResponse.getBufferSize() , then the response will be broadcast. If not, Content-Length will be used.

In your case, just delete the second cleanup code, solving your problem.

+1
source

Source: https://habr.com/ru/post/1480604/


All Articles