Vivek
Vivek

Reputation: 621

Unable to read entire POST request body content formatted as application/json

I've been having an issue with Jetty processing application/json formatted request body data. Essentially, when the request body is processed by Jetty, the request data is cut off.

I have a relatively large POST body of around 74,000 bytes. As per some advice I found online, I instantiated a new context handler with the setMaxFormContentSize property set to a sufficiently large size of 500,000 bytes.

    ServletContextHandler handler = new ServletContextHandler(server, "/");
    handler.setMaxFormContentSize(500000);

However, this did not seem to work correctly. I also read online that this property might only work for form encoded data, not application/json, which is a strict requirement of our application.

Is there any way to circumvent this issue? Is there some special constraint class that I can subclass to allow the processing size to increase to at least 500KB?

Edit #1: I should add that I also tried to drop the size of the limit to 5 bytes to see if it would cut off more of the data in the payload. That also didn't working, which seems to imply that's definitely ignoring the property entirely.

Edit #2: Here is where I read the information from the request stream.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    try {
        String json = CharStreams.toString(new InputStreamReader(req.getInputStream()));
        ....
    } catch (Exception e) {
        logger.error("Exception in internal api forwarder", e);
        throw e;
    }
}

This seems to a standard way of reading from a request stream. I also tried using a BufferedReader from req.getReader() with the same issue.

Vivek

Upvotes: 0

Views: 954

Answers (1)

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49472

What is this CharStreams object?

It doesn't seem to know, or care, or honor the request character encoding. (Bad idea)

Suggest that you use the servlet request.getReader() instead of request.getInputStream() (which is really only designed for binary request body content)

Using request.getReader() will at the very least support your request character encoding properly.

Another bit of information you might want to look into is request.getContentLength() and verify that the request headers does indeed contain the size you are expecting.

Upvotes: 1

Related Questions