Skip to content

Bind: return 413 status code when error is http.MaxBytesError#3841

Closed
ItalyPaleAle wants to merge 4 commits intogin-gonic:masterfrom
ItalyPaleAle:bind-request-entity-too-large
Closed

Bind: return 413 status code when error is http.MaxBytesError#3841
ItalyPaleAle wants to merge 4 commits intogin-gonic:masterfrom
ItalyPaleAle:bind-request-entity-too-large

Conversation

@ItalyPaleAle
Copy link
Contributor

The Go standard library includes a method http.MaxBytesReader that allows limiting the request body. For example, users can create a middleware like:

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).

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).
@appleboy
Copy link
Member

build fail.

@ItalyPaleAle
Copy link
Contributor Author

build fail.

@appleboy looks like it's failing on the master branch too: https://github.com/gin-gonic/gin/actions/runs/7814638661/job/21316374466

Reason is that the "go.mod" file now uses Go 1.20, so the CI fails with 1.18

@codecov
Copy link

codecov bot commented Apr 26, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.99%. Comparing base (3dc1cd6) to head (45a4445).
Report is 107 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3841      +/-   ##
==========================================
- Coverage   99.21%   98.99%   -0.23%     
==========================================
  Files          42       41       -1     
  Lines        3182     3376     +194     
==========================================
+ Hits         3157     3342     +185     
- Misses         17       24       +7     
- Partials        8       10       +2     
Flag Coverage Δ
?
-tags nomsgpack 98.99% <100.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 ?
go-1.23 98.99% <100.00%> (?)
go-1.24 98.99% <100.00%> (?)
macos-latest 98.99% <100.00%> (-0.23%) ⬇️
ubuntu-latest 98.99% <100.00%> (-0.23%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants