fix(transport): check if body is empty before get Content-Type decoder#3654
Open
MikasaAkerman wants to merge 4 commits intogo-kratos:mainfrom
Open
fix(transport): check if body is empty before get Content-Type decoder#3654MikasaAkerman wants to merge 4 commits intogo-kratos:mainfrom
MikasaAkerman wants to merge 4 commits intogo-kratos:mainfrom
Conversation
If body is empty, we may not hav Content-Type header, and will return a CODEC error Close go-kratos#3653
53c5e05 to
0d5c190
Compare
tonybase
reviewed
Apr 29, 2025
| if !ok { | ||
| return errors.BadRequest("CODEC", fmt.Sprintf("unregister Content-Type: %s", r.Header.Get("Content-Type"))) | ||
| } | ||
| data, err := io.ReadAll(r.Body) |
Member
There was a problem hiding this comment.
Member
|
@MikasaAkerman Thanks for your contribution. I think it would be better to add two test cases:
|
Collaborator
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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: