Go Http Request
2021-01-22 01:12
标签:put ring 传输 coder ade read ams sha hal 通过GET、POST或PUT或PATCH向服务端发送JSON Go Http Request 标签:put ring 传输 coder ade read ams sha hal 原文地址:https://www.cnblogs.com/followyou/p/13289574.html传输json
func GetJson(reqUrl string, params map[string]string, header map[string]string) (response HttpResponse) {
response = HttpResponse{
Url: reqUrl,
RequestHeader: header,
}
var err error
defer func() {
response.Error = err
}()
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(params)
req, err := http.NewRequest("GET", reqUrl, b)
if err != nil {
return response
}
for k, v := range header {
req.Header.Add(k, v)
}
resp, err := HttpClient.Do(req)
if err != nil {
return response
}
defer resp.Body.Close()
p, _ := json.Marshal(params)
response.ResponseHeader = resp.Header
response.StatusCode = resp.StatusCode
response.Body, err = ioutil.ReadAll(resp.Body)
return response
}
上一篇:JavaScript