feat: 支持添加自定义 header

This commit is contained in:
Mmx233
2022-10-30 14:11:40 +08:00
parent f5c1af8496
commit f347ca4539
4 changed files with 30 additions and 15 deletions

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"github.com/Mmx233/tool"
log "github.com/sirupsen/logrus"
"io"
"net/http"
"strings"
"time"
@@ -14,6 +15,7 @@ type Api struct {
inited bool
BaseUrl string
Client *http.Client
Header http.Header
}
func (a *Api) Init(https bool, domain string, client *http.Client) {
@@ -41,7 +43,8 @@ func (a *Api) request(path string, query map[string]interface{}) (map[string]int
}
query["callback"] = callback
query["_"] = timestamp
_, res, e := tool.NewHttpTool(a.Client).GetString(&tool.DoHttpReq{
httpTool := tool.NewHttpTool(a.Client)
req, e := httpTool.GenReq("GET", &tool.DoHttpReq{
Url: a.BaseUrl + path,
Query: query,
})
@@ -50,6 +53,24 @@ func (a *Api) request(path string, query map[string]interface{}) (map[string]int
return nil, e
}
for k, v := range a.Header {
req.Header[k] = v
}
resp, e := httpTool.Client.Do(req)
if e != nil {
log.Debugln(e)
return nil, e
}
defer resp.Body.Close()
data, e := io.ReadAll(resp.Body)
if e != nil {
log.Debugln(e)
return nil, e
}
res := string(data)
log.Debugln(res)
res = strings.TrimPrefix(res, callback+"(")
res = strings.TrimSuffix(res, ")")