【问题标题】:POST request with large body received empty on server side在服务器端收到大主体的 POST 请求为空
【发布时间】:2014-01-23 00:50:42
【问题描述】:

当向我的 Play 框架操作方法发出带有大主体的 POST 请求时,我在提取数据时得到 null。如果 body 很小,我可以很好地检索数据。

这是一个简短的数据集示例:

{
  "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
    }
  ]
}

当我在正文中使用此 JSON 发出 POST 请求时,一切正常。但是,如果我在 geometry 数组中添加更多 (~4000) 点,我会在操作中得到 null

这是我的操作方法:

@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()));
    }
}

我尝试使用 chrome 中的 Advanced REST Client 和 ċurl 发送请求,均失败。

可能是什么问题?是不是我需要为大型请求包含一个 Content-Lenght 标头?如果是这样,我该如何手动计算任意 JSON 数据?

【问题讨论】:

    标签: json playframework http-post playframework-2.2 http-content-length


    【解决方案1】:

    请查看 PlayFramework documentation,他们提到请求的默认最大长度为 100KB:

    最大内容长度

    基于文本的正文解析器(例如 text、json、xml 或 formUrlEncoded) 使用最大内容长度,因为它们必须加载所有 内容进入内存。

    有一个默认的内容长度(默认为100KB)。

    提示:默认内容大小可以在application.conf中定义:

    parsers.text.maxLength=128K

    您还可以通过@BodyParser.Of 指定最大内容长度 注释:

    // 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()); 
      }
    }
    

    【讨论】:

    • 是的,我刚刚发现了。现在可以了。不管怎么说,还是要谢谢你。接受并支持我:)
    • 我希望request().body() 在超出内容长度时抛出异常。像这样真的很神秘。
    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 2016-01-18
    • 2016-12-25
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    相关资源
    最近更新 更多