123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- package http
- import (
- "io/ioutil"
- "net/http"
- "strings"
- "time"
- "github.com/pkg/errors"
- "kpt.notice/pkg/log"
- )
- type Client struct {
- client *http.Client
- }
- func NewClient(timeout time.Duration) *Client {
- return &Client{
- client: &http.Client{
- Timeout: timeout,
- },
- }
- }
- func HttpPost(url, contentType, body string) (resp *http.Response, err error) {
- // body := `{"userinfo":"tmr.shengmu23.bbc","openid":"123"}`
- // url :="http://kpttest.kptyun.com/userwxopenid/binding"
- // contentType:="application/json"
- log.Infof("pkg.http.HttpPost{url:%s,contentType:%s,body:%s}", url, contentType, body)
- resp, err = http.Post(url, contentType, strings.NewReader(body))
- if err != nil {
- err = errors.Wrap(err, "http.HttpPost")
- return
- }
- defer resp.Body.Close()
- return
- }
- func HttpGet(url string, params map[string]string) (body []byte, err error) {
- // url :="http://kpttest.kptyun.com/userwxopenid/binding"
- // contentType:="application/json"
- // params := map[string]string{"userinfo":"tmr.shengmu23.bbc","openid":"123"}
- s := make([]string, len(params))
- for k, v := range params {
- s = append(s, k+"="+v)
- }
- url = url + "?" + strings.Join(s, "&")
- resp, e := http.Get(url)
- if e != nil {
- err = errors.Wrap(e, "pkg.http.HttpGet==")
- return
- }
- body, e = ioutil.ReadAll(resp.Body)
- if e != nil {
- err = errors.Wrap(e, "pkg.http.HttpGet.read==")
- return
- }
- defer resp.Body.Close()
- return
- }
|