-
Notifications
You must be signed in to change notification settings - Fork 8.5k
chore(bind): return 413 status code when error is http.MaxBytesError
#4227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(bind): return 413 status code when error is http.MaxBytesError
#4227
Conversation
http.MaxBytesReader that allows limiting the request body. For example, users can create a middleware like:http.MaxBytesError
The Go standard library includes a method `http.MaxBytesReader` that allows limiting the request body. For example, users can create a middleware like:
```go
func MiddlewareMaxBodySize(c *gin.Context) {
// Limit request body to 100 bytes
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 100)
c.Next()
}
```
When the body exceeds the limit, reading from the request body returns an error of type `http.MaxBytesError`.
This PR makes sure that when the error is of kind `http.MaxBytesError`, Gin returns the correct status code 413 (Request Entity Too Large) instead of a generic 400 (Bad Request).
5dcdb08 to
2efd359
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #4227 +/- ##
==========================================
- Coverage 99.21% 98.92% -0.30%
==========================================
Files 42 44 +2
Lines 3182 3432 +250
==========================================
+ Hits 3157 3395 +238
- Misses 17 26 +9
- Partials 8 11 +3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
The test is failing when using "sonic avx" as tags, but I can't repro the error locally... Any advice? |
|
Please rebase the master branch |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR ensures that when a binding error is due to http.MaxBytesError, Gin responds with HTTP 413 instead of 400.
Key changes:
- Update
MustBindWithincontext.goto detecthttp.MaxBytesErrorand return 413 - Add
TestContextBindRequestTooLargeand adjust related tests incontext_test.go - Simplify URL concatenation in
gin_test.go
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| context.go | Detect http.MaxBytesError in MustBindWith and set 413 |
| context_test.go | Replace buffer usage, add test for oversized request binding |
| gin_test.go | Simplify HTTP GET calls by string concatenation |
Done, thanks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR changes Gin’s binding behavior to return a 413 (Request Entity Too Large) status code when the error from reading the request body is of type http.MaxBytesError. In addition, it updates several tests to use strings.NewReader for constructing request bodies and adds a new test (TestContextBindRequestTooLarge) to verify the new behavior.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| context_test.go | Updates to use strings.NewReader for request bodies and addition of a new test for 413. |
| context.go | Updates MustBindWith to handle http.MaxBytesError, returning HTTP 413 instead of 400. |
http.MaxBytesErrorhttp.MaxBytesError
|
@ItalyPaleAle testing error. Please help to take a look. |
I cannot repro locally. Looks like tests pass on other platforms, but fail when the |
|
But the test passes when NOT using sonic: https://github.com/gin-gonic/gin/actions/runs/15143597508/job/42624844475?pr=4227 must be a bug in the sonic library I can disable this test when sonic is used? |
…equest-entity-too-large-2
ecbeb4a to
666545f
Compare
|
This feature won't work when using go-json or sonic due to bugs upstream: goccy/go-json#485 In those cases, gin will continue to return a 400 response. Tests should pass now |
|
@ItalyPaleAle Thanks for your effort. |


The Go standard library includes a method http.MaxBytesReader that allows limiting the request body. For example, users can create a middleware like:
When the body exceeds the limit, reading from the request body returns an error of type
http.MaxBytesError.This PR makes sure that when the error is of kind
http.MaxBytesError, Gin returns the correct status code 413 (Request Entity Too Large) instead of a generic 400 (Bad Request).