1 Star 0 Fork 1

小栈/GoEx

forked from simonxing/GoEx 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
HttpUtils.go 4.57 KB
一键复制 编辑 原始数据 按行查看 历史
nntaoli 提交于 2019-07-25 15:26 . [bitmex.com] api
package goex
//http request 工具函数
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
)
func NewHttpRequest(client *http.Client, reqType string, reqUrl string, postData string, requstHeaders map[string]string) ([]byte, error) {
req, _ := http.NewRequest(reqType, reqUrl, strings.NewReader(postData))
if req.Header.Get("User-Agent") == "" {
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")
}
if requstHeaders != nil {
for k, v := range requstHeaders {
req.Header.Add(k, v)
}
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bodyData, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("HttpStatusCode:%d ,Desc:%s", resp.StatusCode, string(bodyData)))
}
return bodyData, nil
}
func HttpGet(client *http.Client, reqUrl string) (map[string]interface{}, error) {
respData, err := NewHttpRequest(client, "GET", reqUrl, "", nil)
if err != nil {
return nil, err
}
var bodyDataMap map[string]interface{}
err = json.Unmarshal(respData, &bodyDataMap)
if err != nil {
log.Println(string(respData))
return nil, err
}
return bodyDataMap, nil
}
func HttpGet2(client *http.Client, reqUrl string, headers map[string]string) (map[string]interface{}, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
respData, err := NewHttpRequest(client, "GET", reqUrl, "", headers)
if err != nil {
return nil, err
}
var bodyDataMap map[string]interface{}
err = json.Unmarshal(respData, &bodyDataMap)
if err != nil {
log.Println("respData", string(respData))
return nil, err
}
return bodyDataMap, nil
}
func HttpGet3(client *http.Client, reqUrl string, headers map[string]string) ([]interface{}, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
respData, err := NewHttpRequest(client, "GET", reqUrl, "", headers)
if err != nil {
return nil, err
}
//println(string(respData))
var bodyDataMap []interface{}
err = json.Unmarshal(respData, &bodyDataMap)
if err != nil {
log.Println("respData", string(respData))
return nil, err
}
return bodyDataMap, nil
}
func HttpGet4(client *http.Client, reqUrl string, headers map[string]string, result interface{}) error {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
respData, err := NewHttpRequest(client, "GET", reqUrl, "", headers)
if err != nil {
return err
}
err = json.Unmarshal(respData, result)
if err != nil {
log.Printf("HttpGet4 - json.Unmarshal failed : %v, resp %s", err, string(respData))
return err
}
return nil
}
func HttpGet5(client *http.Client, reqUrl string, headers map[string]string) ([]byte, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
respData, err := NewHttpRequest(client, "GET", reqUrl, "", headers)
if err != nil {
return nil, err
}
return respData, nil
}
func HttpPostForm(client *http.Client, reqUrl string, postData url.Values) ([]byte, error) {
headers := map[string]string{
"Content-Type": "application/x-www-form-urlencoded"}
return NewHttpRequest(client, "POST", reqUrl, postData.Encode(), headers)
}
func HttpPostForm2(client *http.Client, reqUrl string, postData url.Values, headers map[string]string) ([]byte, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
return NewHttpRequest(client, "POST", reqUrl, postData.Encode(), headers)
}
func HttpPostForm3(client *http.Client, reqUrl string, postData string, headers map[string]string) ([]byte, error) {
return NewHttpRequest(client, "POST", reqUrl, postData, headers)
}
func HttpPostForm4(client *http.Client, reqUrl string, postData map[string]string, headers map[string]string) ([]byte, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/json"
data, _ := json.Marshal(postData)
return NewHttpRequest(client, "POST", reqUrl, string(data), headers)
}
func HttpDeleteForm(client *http.Client, reqUrl string, postData url.Values, headers map[string]string) ([]byte, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
return NewHttpRequest(client, "DELETE", reqUrl, postData.Encode(), headers)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xiaozhan/GoEx.git
git@gitee.com:xiaozhan/GoEx.git
xiaozhan
GoEx
GoEx
master

搜索帮助