发送post请求
reqMap := make(map[string]interface{})
reqMap["order_num"] = request.OutTradeNo
reqMap["transaction_id"] = request.TransactionId
reqMap["pay_time"] = request.SuccessTime
reqMapStr, _ := json.Marshal(reqMap)
reader := bytes.NewReader(reqMapStr)
url := "https://xxx.xxx.com/api/xxx/xxx/getNotify" //要访问的Url地址
resp, err := http.NewRequest("POST", url, reader)
defer resp.Body.Close() //程序在使用完回复后必须关闭回复的主体
if err != nil {
return nil, status.Errorf(codes.Unknown, "xxx")
}
resp.Header.Set("Content-Type", "application/json;charset=UTF-8")
client := http.Client{}
resp1, err := client.Do(resp) //Do 方法发送请求,返回 HTTP 回复
if err != nil {
return nil, status.Errorf(codes.Unknown, "xxx")
}
respBytes, err := ioutil.ReadAll(resp1.Body)
if err != nil {
return nil, status.Errorf(codes.Unknown, "xxx")
}
rspStr := (*string)(unsafe.Pointer(&respBytes))
if "success" != *rspStr {
return nil, status.Errorf(codes.Unknown, "xxx")
}
标签:请求,err,nil,Unknown,resp,xxx,reqMap,发送,Go
From: https://www.cnblogs.com/wangjinkui/p/16985325.html