【问题标题】:Remove Transfer-Encoding:chunked in the POST request?删除 POST 请求中的 Transfer-Encoding:chunked?
【发布时间】:2016-02-04 11:47:49
【问题描述】:

我正在使用以下代码发送POST 请求,但该请求以分块(Transfer-Encoding: chunked)的形式发送。我用谷歌搜索了这个问题,它说要包含Content-Length,但在下面的代码中我不知道如何设置Content-Length

@RequestMapping(value = "/contacts", method = RequestMethod.POST)
public Map<String, ContactInfo> addContactInfo(
                                @RequestBody Map<String, ContactInfo> ContactInfoDto) {

    ContactInfo contactInfo = ContactInfoDto.get("contact");
    if (contactInfo == null) {
        throw new IllegalArgumentException("Contact not found.");
    }

    contactInfo = this.contactInfoManager.addNew(contactInfo);
    Map<String, ContactInfo> map = new HashMap<>();
    map.put("contact", contactInfo);

    return map;

}

【问题讨论】:

  • 你得到答案了吗?
  • 我相信是响应,而不是请求,它是以面向块的方式传输的
  • @RamDuttShukla 是请求还是响应你要设置Content-Length就可以了?
  • @AliDehghani 作为回应。

标签: java spring spring-mvc


【解决方案1】:

您可以使用ResponseEntity 明确设置标题。棘手的一点是弄清楚你的内容实际上有多长:

@RequestMapping(value = "/contacts", method = RequestMethod.POST)
public ResponseEntity<Map<String, ContactInfo>> addContactInfo(@RequestBody Map<String, ContactInfo> contactInfoDto) throws JsonProcessingException {

    ContactInfo contactInfo = contactInfoDto.get("contact");
    if (contactInfo == null) {
        throw new IllegalArgumentException("Contact not found.");
    }

    contactInfo = this.contactInfoManager.addNew(contactInfo);
    Map<String, ContactInfo> map = new HashMap<>();
    map.put("contact", contactInfo);

    HttpHeaders headers = new HttpHeaders();
    headers.set(HttpHeaders.CONTENT_LENGTH, String.valueOf(new ObjectMapper().writeValueAsString(map).length()));
    return new ResponseEntity<Map<String, ContactInfo>>(map, headers, HttpStatus.CREATED);
}

测试:

$ curl -v http://localhost:8080/contacts/ -X POST -d '{ "contact": { "name": "foo" } }' -H 'Content-Type: application/json' && echo
*   Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> POST /contacts/ HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 32
> 
* upload completely sent off: 32 out of 32 bytes
< HTTP/1.1 201 Created
< Server: Apache-Coyote/1.1
< X-Application-Context: application
< Content-Type: application/json;charset=UTF-8
< Content-Length: 26
< Date: Fri, 10 Jun 2016 13:24:23 GMT
< 
* Connection #0 to host localhost left intact
{"contact":{"name":"foo"}}

【讨论】:

  • 谢谢。这有助于解决我的问题。
【解决方案2】:

以下代码:

@RequestMapping(value = "/contacts", method = RequestMethod.POST)
public Map<String, ContactInfo> addContactInfo(
                                @RequestBody Map<String, ContactInfo> ContactInfoDto,    
                                @RequestHeader(value = HttpHeaders.CONTENT_LENGTH, required = true) Long contentLength
) { ... }

可用于要求发送Content-Length 标头。 请注意,您还必须在发送请求的代码中添加该标头(大多数客户端会自动执行此操作,但更好地检查)

【讨论】:

    猜你喜欢
    • 2012-05-31
    • 1970-01-01
    • 2012-05-19
    • 2023-02-10
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多