Go Http Request

2021-01-22 01:12

阅读:439

标签:put   ring   传输   coder   ade   read   ams   sha   hal   

目录
  • 传输json

传输json

通过GET、POST或PUT或PATCH向服务端发送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
}

Go Http Request

标签:put   ring   传输   coder   ade   read   ams   sha   hal   

原文地址:https://www.cnblogs.com/followyou/p/13289574.html

上一篇:JavaScript

下一篇:ASP.NET Core读取配置文件


评论


亲,登录后才可以留言!