HTTP请求header之Content-Length

首先说什么是Content-Length,在http的协议中Content-Length头部告诉浏览器报文中实体数据的大小。

说到对Content-Length的关注源于一个项目报错信息

Could not read document: Unexpected end-of-input in VALUE_STRING
at [Source: java.io.PushbackInputStream@331dba5d; line: 4, column: 82]
at [Source: java.io.PushbackInputStream@331dba5d; line: 4, column: 14] (through reference chain: com.byron.service.user.model.UserLoginModel["loginType"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected end-of-input in VALUE_STRING
at [Source: java.io.PushbackInputStream@331dba5d; line: 4, column: 82]
at [Source: java.io.PushbackInputStream@331dba5d; line: 4, column: 14] (through reference chain: com.byron.service.user.model.UserLoginModel["loginType"])

Java代码中看loginType是枚举类型

@NotBlank(message = "账号不能为空")
 private String username;
 @NotBlank(message = "密码不能为空")
 private String password;

private LoginType loginType =LoginType.pwd;

入参:

header
Content-Type:application/json
Content-Length:64
body
{
    "username":"1322225678",
    "password":"11",
    "loginType":"pwd"
}

报错原因:

header中配置Content-Length参数,由于配置过小,所以JSON实体被自动截断了,导致最后参数无法匹配上.

修正:

最好不使用Content-Length请求头,或者设置大一点即可

参考:

https://segmentfault.com/a/1190000011920471

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。