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

add form:"-" to ignore bind is not working #3711

Open
hotcoffie opened this issue Sep 4, 2023 · 2 comments
Open

add form:"-" to ignore bind is not working #3711

hotcoffie opened this issue Sep 4, 2023 · 2 comments

Comments

@hotcoffie
Copy link

hotcoffie commented Sep 4, 2023

its not working properly now.
When I added form:"-" to my field,
the field is still being assigned a value regardless of whether I use Bind or ShouldBind.

go 1.21
github.com/gin-gonic/gin v1.9.1

type Astruct struct {
	Aa string  `form:"-" json:"aa"`
	Bb *string `form:"-" json:"bb"`
}

func Init(r *gin.Engine) {

	api := r.Group("/api")
	{
		api.GET("/ping", func(c *gin.Context) {
			c.JSON(http.StatusOK, "pong")
		})

		api.POST("/test", func(ctx *gin.Context) {
			var a Astruct
			ctx.Bind(&a)
			fmt.Println(a.Aa)
			fmt.Println(*a.Bb)
			ctx.AbortWithStatus(http.StatusOK)
		})
	}
}
POST http://127.0.0.1:9527/api/test
Content-Type: application/json

{
	"aa": "a",
	"bb": "b"
}
a
b
2023-09-05T01:30:16.577+0800	DEBUG	utils/logger.go:43	gin	{"method": "POST", "path": "/api/test", "status": 200, "ip": "127.0.0.1", "elapsed": "243.845µs"}

Originally posted by @hotcoffie in #1733 (comment)

@filbertmangiri
Copy link

2 weeks and no responses, BUMP

I'm also facing this problem..

@zanmato
Copy link

zanmato commented Sep 22, 2023

In your example you are POSTing json which is still being bound via json:"aa". form is for form-data requests

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

No branches or pull requests

3 participants