Go 如何发出HTTP post数据请求
此req.Param错误:复合文字中缺少类型 此字符串参数不能使用变量user和msgGo 如何发出HTTP post数据请求,go,Go,此req.Param错误:复合文字中缺少类型 此字符串参数不能使用变量user和msg 如何使用变量param?假设您使用的是“net/http”,Post函数要求url、内容类型和正文。链接: 因此,对于您的特定情况,它应该是这样的 func send_msg(msg string, user string) { url := "https://test.com/" token := "Bearer " + get_token() header := req.Hea
如何使用变量param?假设您使用的是
“net/http”
,Post函数要求url
、内容类型
和正文
。链接:
因此,对于您的特定情况,它应该是这样的
func send_msg(msg string, user string) {
url := "https://test.com/"
token := "Bearer " + get_token()
header := req.Header{
"Content-Type": "application/json",
"Authorization": token,
}
// this string param can not use variable user and msg.
// param := `{
// "email": "jjjj@gmail.com",
// "msg_type": "text",
// "content": { "text": "aaaaaaaaaaaaa" }
// }`
// this req.Param param error:missing type in composite literal
param := req.Param{
"email": user,
"msg_type": "text",
"content": { "text" : msg },
}
r,_ := req.Post(url, header, param)
resp := r.String()
log.Println(resp)
}
如果要添加自定义标题,可以使用提供req
的,然后可以添加或设置自定义标题
url := "https://test.com/"
contentType := "application/json"
param := []byte(`{"email": user, "msg_type": "text","content": { "text" : "msg" }, }`)
resp, err := http.Post(url, contentType, bytes.NewBuffer(param))
defer resp.Body.Close()
if err != nil {
//handle error
} else {
body, _ := ioutil.ReadAll(resp.Body)
log.Println(body)
}
这回答了你的问题吗?
req.Headers.Set(existingHeaderWithNewValue)
req.Headers.Add(customHeader)