seriousgeek seriousgeek - 10 months ago 322
Java Question

Request header too large while sending image in REST response

I have a REST service developed using Spring Boot that is attempting to send back image data to the client, which would then be displayed in the browser. However when i attempt this, i get the following error:

2016-09-26 08:40:31.897 INFO 6435 --- [nio-8080-exec-7] o.a.coyote.http11.Http11NioProcessor : Error parsing HTTP request header
Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.

java.lang.IllegalArgumentException: Request header is too large
at org.apache.coyote.http11.InternalNioInputBuffer.fill( ~[tomcat-embed-core-8.0.36.jar!/:8.0.36]
at org.apache.coyote.http11.AbstractNioInputBuffer.parseRequestLine( ~[tomcat-embed-core-8.0.36.jar!/:8.0.36]
at org.apache.coyote.http11.AbstractHttp11Processor.process( ~[tomcat-embed-core-8.0.36.jar!/:8.0.36]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process( [tomcat-embed-core-8.0.36.jar!/:8.0.36]
at$SocketProcessor.doRun( [tomcat-embed-core-8.0.36.jar!/:8.0.36]
at$ [tomcat-embed-core-8.0.36.jar!/:8.0.36]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_60]
at java.util.concurrent.ThreadPoolExecutor$ Source) [na:1.8.0_60]
at org.apache.tomcat.util.threads.TaskThread$ [tomcat-embed-core-8.0.36.jar!/:8.0.36]
at Source) [na:1.8.0_60]

My service code is as follows:

@RequestMapping(method = RequestMethod.POST, value = "/com")
public String handleCompare(@RequestParam("testid") String test,
RedirectAttributes redirectAttributes, Model model) throws IOException {

File f=new File("Sampletest.jpg");
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ImageIO.write(origImg, "jpg", baos );
byte[] imageInByte=baos.toByteArray();
byte[] encoded= Base64.encodeBase64(imageInByte);
String encodedString = new String(encoded);
model.addAttribute("webcamattr", encodedString);
return "resp";

The relevant part of "resp.html" is as follows:

<td align="center"><b>Response Image</b></td>
<tr th:each="webfile : ${webcamattr}">
<td> <img width="300" height="300" src="data:image/jpeg;base64" th:src="${webcamattr}"></img></td>

My file is as follows:


The file that I'm trying to send is just 19KB in size but I'm not sure why it is throwing a large request header error. Could you please help in identifying what am I doing wrong? Is the image data being passed in the request header? If so, how can i send it in the response body?


Answer Source

Your problem is not the from the request to .../com that invokes the handleCompare() method. That request completes normally, and sends an HTML page to the client browser.

The client browser will then try to render that page, and will see the <img width="300" height="300" src="sojghsirhsdfoh...bytes removed...daskfgdskfg=="></img> tag. The browser will therefore send another request to the server to URL .../sojghsirhsdfoh...bytes removed...daskfgdskfg== to get the data for the image to be displayed.

That request is causing the error, because the URL to too long. This is backed by the callstack showing the error occurs during a call to parseRequestLine().

The src attribute of an <img> tag is built incorrectly. If you intended to build a Data URI, the data:image/jpeg;base64, should be a prefix to the value, but th:src replaces the src attribute. Also notice that you need a ,.

If possible, you should avoid using Data URIs. Define another URL for retrieving the JPEG image, and change the src attribute to be that URL. Especially if the image is static, like your code shows.