gin-gonic / gin

Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
https://gin-gonic.com/
MIT License
78.97k stars 8.02k forks source link

Binding URI and JSON on the same struct #2758

Open tunhuit opened 3 years ago

tunhuit commented 3 years ago

I want to bind URI params and JSON payload in the same struct, How can I do this?

type UpdateRequest struct {
    ID   uint64 `uri:"id" binding:"required"`
    Data struct {
        Name string `json:"name" binding:"required"`
        Slug string `json:"slug" binding:"required"`
    }
}
masterZSH commented 3 years ago

`

    var params UpdateRequest

    c.ShouldBindUri(&params)

    c.ShouldBindJSON(&params.Data)

`

kszafran commented 3 years ago

Related PR: https://github.com/gin-gonic/gin/pull/2812