golang发送GET、POST、PUT、DELETE请求
package request
import (
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
)
type request interface {
GET() (ret *result, err error)
POST() (ret *result, err error)
PUT() (ret *result, err error)
DELETE() (ret *result, err error)
}
type Url string
type Param map[string]interface{}
// 开启终端调试 true or false
var DEBUG bool
type req struct {
Url
Param
}
type result struct {
Url
Param
Method string
Params interface{}
Body string
Header http.Header
Response *http.Response
Proto string
Host string
URL *url.URL
Status string
StatusCode int
}
// GET
func (r req) GET() (ret *result, err error) {
return r.do("GET", r.Url, r.Param)
}
// POST
func (r req) POST() (ret *result, err error) {
return r.do("POST", r.Url, r.Param)
}
// PUT
func (r req) PUT() (ret *result, err error) {
return r.do("PUT", r.Url, r.Param)
}
// DELETE
func (r req) DELETE() (ret *result, err error) {
return r.do("DELETE", r.Url, r.Param)
}
// 模拟请求
func (r req) do(method string, url Url, param Param) (ret *result, err error) {
reqs, err := http.NewRequest(method, string(url), nil)
if err != nil {
return nil, err
}
// 添加header
r.setHeader(reqs)
// 设置参数
q := reqs.URL.Query()
for k, v := range param {
q.Add(k, fmt.Sprint(v))
}
reqs.URL.RawQuery = q.Encode()
res, err := http.DefaultClient.Do(reqs)
if err != nil {
return nil, err
}
defer res.Body.Close()
ret = &result{
Url: r.Url,
Param: r.Param,
Method: method,
Body: r.getBody(res),
Header: reqs.Header,
Response: res,
Proto: reqs.Proto,
Host: reqs.Host,
URL: reqs.URL,
Status: res.Status,
StatusCode: res.StatusCode,
}
r.debug(ret)
return
}
func (r req) getBody(res *http.Response) (body string) {
if res.StatusCode == 200 {
switch res.Header.Get("Content-Encoding") {
case "gzip":
reader, _ := gzip.NewReader(res.Body)
for {
buf := make([]byte, 1024)
n, err := reader.Read(buf)
if err != nil && err != io.EOF {
panic(err)
}
if n == 0 {
break
}
body += string(buf)
}
default:
bodyByte, _ := ioutil.ReadAll(res.Body)
body = string(bodyByte)
}
} else {
bodyByte, _ := ioutil.ReadAll(res.Body)
body = string(bodyByte)
}
return
}
func (r req) setHeader(h *http.Request) {
h.Header.Add("Authorization", "token_value")
h.Header.Add("Content-Type", "text/plain; charset=UTF-8")
h.Header.Add("User-Agent", "Go-http-client/1.14")
h.Header.Add("Transfer-Encoding", "chunked")
h.Header.Add("Accept-Encoding", "gzip, deflate")
}
// 打印请求参数及返回信息
func (r req) debug(ret *result) {
if DEBUG {
log.Println( "debug log start ----------")
fmt.Println(ret.Method, ret.Proto)
fmt.Println("Host", ":", ret.Host)
fmt.Println("URL", ":", ret.URL)
fmt.Println("RawQuery", ":", ret.URL.RawQuery)
for key, val := range ret.Header {
fmt.Println(key, ":", val[0])
}
fmt.Println("----------------------------------------------------")
fmt.Println("Status", ":", ret.Status)
for key, val := range ret.Response.Header {
fmt.Println(key, ":", val[0])
}
log.Println("debug log end ----------")
}
}
// GET
func GET(url Url, param Param) (ret *result, err error) {
return req.GET(req{
Url: url,
Param: param,
})
}
// POST
func POST(url Url, param Param) (ret *result, err error) {
return req.POST(req{
Url: url,
Param: param,
})
}
// PUT
func PUT(url Url, param Param) (ret *result, err error) {
return req.PUT(req{
Url: url,
Param: param,
})
}
// DELETE
func DELETE(url Url, param Param) (ret *result, err error) {
return req.DELETE(req{
Url: url,
Param: param,
})
}
测试请求
// 开启debug
request.DEBUG = true
url := "http://www.beego.cn/api/demo"
r, err := request.GET(request.Url(url), request.Param{"id": 20, "name": "赵云"})
if err == nil {
fmt.Println(r.Body)
}
r, err = request.POST(request.Url(url), request.Param{"id": 22, "name": "张飞"})
if err == nil {
fmt.Println(r.Body)
}
r, err = request.PUT(request.Url(url), request.Param{"id": 23, "name": "马超"})
if err == nil {
fmt.Println(r.Body)
}
r, err = request.DELETE(request.Url(url), request.Param{"id": 24, "name": "关羽"})
if err == nil {
fmt.Println(r.Body)
}