POST request with large body received empty on server side

2.4k views Asked by At

When issuing a POST request with a large body to my Play framework action method, I get null when extracting the data. If the body is fairly small, I can retrieve the data just fine.

Here's a sample short data set:

{
  "creator": "zoltan",
  "sport": "hike",
  "geometry": [
    {
      "time": "2009-07-10 12:56:10 +0000",
      "x": 10.275514,
      "y": 47.514749,
      "z": 756.587
    },
    {
      "time": "2009-07-10 12:56:19 +0000",
      "x": 10.275563,
      "y": 47.514797,
      "z": 757.417
    }
  ]
}

When I issue a POST request with this JSON in the body, everything works fine. However, if I add many more (~4000) points in the geometry array, I get null in the action.

Here's my action method:

@Transactional
//@BodyParser.Of(Json.class) // tried with this as well
public static Result createTour() {
    LOG.debug("Raw request body: " + request().body().asText());
    JsonNode node = request().body().asJson();
    LOG.debug("JSON request body: " + node);
    TourDto tourDto;
    try {
        tourDto = jsonToTour(node);
        int id = TourDataAccessUtils.create(tourDto);
        return created(toJson(id));
    } catch (JsonProcessingException e) {
        LOG.error("While parsing JSON request.", e);
        return Results.badRequest(
                toJson(Throwables.getRootCause(e).getMessage()));
    }
}

I tried using both Advanced REST Client in chrome and ċurl to send the request, both failed.

What could be the problem? Could it be that I need to include a Content-Lenght header for large requests? If so, how can I manually calculate it for arbitrary JSON data?

1

There are 1 answers

2
meda On BEST ANSWER

Please check the PlayFramework documentation, They mentionned that the default maximum length for request is 100KB:

Max content length

Text based body parsers (such as text, json, xml or formUrlEncoded) use a max content length because they have to load all the content into memory.

There is a default content length (the default is 100KB).

Tip: The default content size can be defined in application.conf:

parsers.text.maxLength=128K

You can also specify a maximum content length via the @BodyParser.Of annotation:

// Accept only 10KB of data.
@BodyParser.Of(value = BodyParser.Text.class, maxLength = 10 * 1024)
pulic static Result index() {
  if(request().body().isMaxSizeExceeded()) {
    return badRequest("Too much data!");
  } else {
    ok("Got body: " + request().body().asText()); 
  }
}