Go Http Request
Posted followyou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Go Http Request相关的知识,希望对你有一定的参考价值。
目录
传输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的主要内容,如果未能解决你的问题,请参考以下文章
解决go: go.mod file not found in current directory or any parent directory; see ‘go help modules‘(代码片段
C#-WebForm-★内置对象简介★Request-获取请求对象Response相应请求对象Session全局变量(私有)Cookie全局变量(私有)Application全局公共变量Vi(代码片段