golang request body query 请求方式封装
创建一个 requests 目录,在requests目录下创建一个request.go文件,内容如下
package requests import ( "bytes" "encoding/json" "fmt" "io" "io/ioutil" "net/http" ) func checkError(err error) { if err != nil { fmt.Println(err) } } type Request struct { Url *string Method string Data *map[string]string Headers *map[string]string RepInfo map[string]interface{} } func (r *Request) Body() { data, err := json.Marshal(r.Data) checkError(err) client := &http.Client{} req, err := http.NewRequest(r.Method, *r.Url, bytes.NewBuffer(data)) checkError(err) req.Header.Add("Content-Type", "application/json;charset=UTF-8") for k, v := range *r.Headers { req.Header.Add(k, v) } rep, err := client.Do(req) checkError(err) defer func(Body io.ReadCloser) { err := Body.Close() checkError(err) }(rep.Body) body, err := ioutil.ReadAll(rep.Body) checkError(err) if err = json.Unmarshal(body, &r.RepInfo); err != nil { fmt.Println(err) } } func (r *Request) Query() { client := &http.Client{} req, err := http.NewRequest(r.Method, *r.Url, nil) checkError(err) req.Header.Add("Content-Type", "application/json;charset=UTF-8") for k, v := range *r.Headers { req.Header.Add(k, v) } query := req.URL.Query() for k, v := range *r.Data { query.Add(k, v) } req.URL.RawQuery = query.Encode() rep, err := client.Do(req) checkError(err) _ = rep.Body.Close() body, err := ioutil.ReadAll(rep.Body) checkError(err) err = json.Unmarshal(body, &r.RepInfo) checkError(err) }
main.go 内容根据自己的真实的API接口进行测试,示例内容如下
package main import ( "requests" "fmt" ) func main() { url := "http://xxxxxxxxx.com/v1/api" data := map[string]string{ "name": "zhangsan", "massage": "这是一个测试消息", } headers := map[string]string{"Authorization": "6c38874672f28b8a11"} request := requests.Request{Url: &url, Method: "POST", Data: &data, Headers: &headers} request.Body() fmt.Println(request.RepInfo) }