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

a temporary fix for issue 4120 #4122

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

Conversation

alsammak-array
Copy link

@alsammak-array alsammak-array commented Dec 26, 2024

Fixes #4120

a workaround is to read the body and reset it to {} inside BindJSON(args)

// BindJSON is a shortcut for c.MustBindWith(obj, binding.JSON).
func (c *Context) BindJSON(obj any) error {

	body, err := io.ReadAll(c.Request.Body)
	if err != nil {
		return fmt.Errorf("failed to read body: %v", err)
	}

	// If the body is empty, set it to {}
	if len(body) == 0 {
		body = []byte("{}")
		c.Request.Body = io.NopCloser(bytes.NewBuffer(body)) // Set to {} to trigger the error
		return c.MustBindWith(obj, binding.JSON)
	}

	// Restore the body since it was read
	c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
	return c.MustBindWith(obj, binding.JSON)
}

the output should be as expected in the issue.

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.

[Feature]: ShouldBindJSON return EOF when the request body is an empty string.
1 participant