Skip to content
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

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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

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.

None yet

2 participants