fix: Remove content-length header to resolve multipart/form-data request body mismatch issue #364
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
问题现象:
在重放
multipart/form-data
请求时,由于不同工具生成的请求体内容、分隔符和元数据存在差异,导致Content-Length
计算不一致,进而引发too many bytes written
错误。问题原理:
multipart/form-data
请求体的结构复杂,包含分隔符、字段元数据和字段值。不同工具(如浏览器、Postman、编程库)在生成请求体时,可能会使用不同的分隔符、换行符或元数据格式,导致请求体的实际字节长度与Content-Length
不匹配。修改方式:
在
FormDataHttpRequestBuilder
的buildRequestContent
方法中,移除请求头中的content-length
,避免手动设置Content-Length
导致的不一致问题。Spring 框架会自动计算并设置正确的Content-Length
。修改内容:
HttpHeaders
中的content-length
,确保请求体长度由框架自动管理。测试结果:
multipart/form-data
请求不再出现too many bytes written
错误。