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

@kagaya85
Copy link
Member

kagaya85 commented Nov 24, 2025

@MikasaAkerman Thanks for your contribution. I think it would be better to add two test cases:

  • one with a request with body nil
  • one with a non-empty request body but without a Content-Type header.

@kratos-ci-bot
Copy link
Collaborator

⚠️ Non-English comments were found in the following locations:


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

4 participants