Request.go 1.36 KB
Newer Older
吴贤德's avatar
吴贤德 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
package Request

import (
	"github.com/imroc/req"
	log "github.com/sirupsen/logrus"
	"time"
)

type Request struct {
}

func init() {
	req.Debug =false
}

//post request
func Post(url string, jsonBody string) (string, error) {
	//log.Infof("request: %s, post:%s \n",url,jsonBody)
	//req.Debug = true
	req.SetFlags(req.LstdFlags | req.Lcost)
	req.SetTimeout(time.Duration(60) * time.Second)

	authHeader := req.Header{
		"Content-Type": "application/json;charset=utf-8",
		"Accept":       "application/json",
		// "Authorization": auth,
	}

	r, err := req.Post(url, authHeader, jsonBody)
	// fmt.Printf("resp: %+v", r)
	if err != nil {
		//fmt.Println("query err:", err, " url:", url, " json:", jsonBody)
		log.Errorln("query err:", err, " url:", url, " json:", jsonBody)
		return "", err
	} else {
		return r.String(), err
	}
}

func Get(url string) (string, error) {
	//log.Infoln("request : ",url)
42
	//req.Debug = true
吴贤德's avatar
吴贤德 committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
	req.SetFlags(req.LstdFlags | req.Lcost)
	req.SetTimeout(time.Duration(60) * time.Second)

	authHeader := req.Header{
		"Content-Type": "application/json;charset=utf-8",
		"Accept":       "application/json",
		// "Authorization": auth,
	}

	r, err := req.Get(url, authHeader)
	// fmt.Printf("resp: %+v", r)
	if err != nil {
		//fmt.Println("query err:", err, " url:", url)
		log.Errorln("query err:", err, " url:", url)
		return "", err
	} else {
		return r.String(), err
	}
}