go http請求

技术颜良發表於2024-06-12
func Send(notification model.Notification, defaultRobot string) (err error) {

markdown, robotURL, err := transformer.TransformToMarkdown(notification)

if err != nil {
return
}

data, err := json.Marshal(markdown)
if err != nil {
return
}

var wechatRobotURL string

if robotURL != "" {
wechatRobotURL = robotURL
} else {
wechatRobotURL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=" + defaultRobot
}

req, err := http.NewRequest(
"POST",
wechatRobotURL,
bytes.NewBuffer(data))

if err != nil {
return
}

req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)

if err != nil {
return
}

defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)

return
}

相關文章