Skip to content

Conversation

MikasaAkerman
Copy link

If body is empty, we cannot get Content-Type, and will return a CODEC error

Close #3653

Description (what this PR does / why we need it):

默认的http.DefaultRequestDecoder会根据请求的Content-Type获取codec,但是body为空,请求头可能不会携带Content-Type这个header,因此服务会响应unregister Content-Type: 的错误。

我把body为空的校验调整到了获取Content-Type之前,避免这种情况出现错误。

Which issue(s) this PR fixes (resolves / be part of):

fixes #3653

Other special notes for the reviewers:

@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Apr 27, 2025
If body is empty, we may not hav Content-Type header, and will return a CODEC error

Close go-kratos#3653
if !ok {
return errors.BadRequest("CODEC", fmt.Sprintf("unregister Content-Type: %s", r.Header.Get("Content-Type")))
}
data, err := io.ReadAll(r.Body)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Server return unregister Content-Type: errors for empty body requests

2 participants