I am now developing a REST API with Gin web Framework.
I would like to bind some fields to different name between PUT and GET as below.
type (
MyData struct {
Col1 string // Client will send this data with `aaa` in PUT, Server will return this data with `bbb` in GET.
}
)
func PutData(c *gin.Context) {
var data MyData
err := c.BindJSON(&data) // Request from client is {"aaa": "some string"},
// Expectation is data.Col1 = "some string"
// Save data to DB
:
:
}
func GetData(c *gin.Context) {
var data MyData
// Retrieve data from DB
data.Col1 = "(data from DB)"
c.JSON(http.StatusOK, data) // Response should be {"bbb": "(data from DB)"}
}
The field aaa
in PUT
should be mapped to Col1
, and this field should be mapped to bbb
in GET
.
If both GET
and PUT
are same field name, I know I can do this like Col1 string `json:"aaa"`
. But my requirement is to map to different name between GET
and PUT
.
Can I do this with Gin?