【发布时间】:2022-08-17 18:10:01
【问题描述】:
我是 golang 的新手,在使用请求正文修改数据结构时遇到问题,在此代码中我想从请求正文修改 var To 基于值,该怎么做? 这里是示例代码https://go.dev/play/p/25GaCvJo84I
正文:{\"phone\": \"1989876787\"}
type Payload struct {
MessagingProduct string `json:\"messaging_product\"`
To string `json:\"to\"`
}
func send(c *gin.Context) {
url := os.Getenv(\"URL\")
accessToken := os.Getenv(\"ACCESS_TOKEN\")
jsonData, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
// Handle error
}
fmt.Println(\"payload\", string(jsonData))
data := Payload{
MessagingProduct: \"whatsapp\",
To: \"\", // how to replace this with request body
}
jsonStr, _ := json.Marshal(data)
req, err := http.NewRequest(\"POST\", url, bytes.NewBuffer(jsonStr))
req.Header.Set(\"Authorization\", \"Bearer \"+accessToken)
req.Header.Set(\"Content-Type\", \"application/json\")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
}
标签: go