I am new in golang and i am having problem when modify data struct with request body, in this code i want to modify var To based value from request body, how to do that?
body: {"phone": "1989876787"}
type Payload struct {
MessagingProduct string `json:"messaging_product"`
To string `json:"to"`
}
func Send(c *gin.Context) {
data := Payload{
MessagingProduct: "sms",
To: "", //modify this from req body
}
jsonStr, _ := json.Marshal(data)
fmt.Println("req body", string(jsonStr))
}
CodePudding user response:
Using gin framework, You can use binding function.
type Payload struct {
Phone string `json:"phone"`
}
func Send(c *gin.Context) {
...
var payload Payload
if err := c.ShouldBindJSON(&payload); err != nil {
// handling error binding
}
data := Payload{
MessagingProduct: "sms",
To: paylod.Phone,
}
...
}