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

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

Open
tttoad opened this issue Dec 23, 2024 · 0 comments · May be fixed by #4122
Open

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

tttoad opened this issue Dec 23, 2024 · 0 comments · May be fixed by #4122

Comments

@tttoad
Copy link

tttoad commented Dec 23, 2024

  • With issues:
    • Use the search tool before opening a new issue.
    • Please provide source code and commit sha if you found a bug.
    • Review existing issues and provide feedback or react to them.

Description

ShouldBindJSON return EOF when the request body is an empty string.
This error message is not friendly.

How to reproduce

package main

import (
	"fmt"

	"github.com/gin-gonic/gin"
)

type R struct {
	C string `form:"c" binding:"required"`
}

func main() {
	g := gin.Default()
	g.POST("/hello", func(c *gin.Context) {
		if err := c.BindJSON(&R{}); err != nil {
			fmt.Println(err, len(c.Errors))
		}
	})
	g.Run(":9000")
}

Expectations

$ curl --request POST '127.0.0.1:9000/hello' --data '' 

Actual result

print EOF 1.
Expect: 
`Key: 'R.C' Error:Field validation for 'C' failed on the 'required' tag 0` or `invalid request` or `empty body`


Environment

  • go version:
  • gin version (or commit ref):
  • operating system:
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 a pull request may close this issue.

1 participant