Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
730e3462f6 | ||
|
|
ae3982faaa | ||
|
|
166fe96265 | ||
|
|
8a0a21fc5e | ||
|
|
025f911c0e | ||
|
|
21a167304b | ||
|
|
54b481c9f5 | ||
|
|
408995f2b4 | ||
|
|
4b77382218 | ||
|
|
13dfdee51c | ||
|
|
01df404ead | ||
|
|
8383a3820f | ||
|
|
3ee03a1c7b | ||
|
|
a68be94c7f |
35
base.go
35
base.go
@@ -7,33 +7,46 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
func buildBodyRequest(ver, rawurl string, body *Body) *http.Request {
|
func buildBodyRequest(wf *Workflow) *http.Request {
|
||||||
var req *http.Request
|
var req *http.Request
|
||||||
var err error
|
var err error
|
||||||
|
contentType := ""
|
||||||
|
|
||||||
if body.IOBody == nil {
|
if wf.Body.IOBody == nil {
|
||||||
req, err = http.NewRequest(ver, rawurl, nil)
|
req, err = http.NewRequest(wf.Method, wf.GetStringURL(), nil)
|
||||||
} else {
|
} else {
|
||||||
var bodybuf *bytes.Buffer
|
var bodybuf *bytes.Buffer
|
||||||
switch body.IOBody.(type) {
|
switch wf.Body.IOBody.(type) {
|
||||||
case []byte:
|
case []byte:
|
||||||
bodybuf = bytes.NewBuffer(body.IOBody.([]byte))
|
bodybuf = bytes.NewBuffer(wf.Body.IOBody.([]byte))
|
||||||
case *bytes.Buffer:
|
case *bytes.Buffer:
|
||||||
bodybuf = bytes.NewBuffer(body.IOBody.(*bytes.Buffer).Bytes())
|
bodybuf = bytes.NewBuffer(wf.Body.IOBody.(*bytes.Buffer).Bytes())
|
||||||
default:
|
default:
|
||||||
panic(errors.New("the type is not exist, type is" + reflect.TypeOf(body.IOBody).String()))
|
panic(errors.New("the type is not exist, type is" + reflect.TypeOf(wf.Body.IOBody).String()))
|
||||||
}
|
}
|
||||||
req, err = http.NewRequest(ver, rawurl, bodybuf)
|
req, err = http.NewRequest(wf.Method, wf.GetStringURL(), bodybuf)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if body.ContentType == "" {
|
if wf.Body.ContentType != "" {
|
||||||
req.Header.Set(HeaderKeyContentType, TypeURLENCODED)
|
if wf.Body.ContentType == TypeContentEmpty {
|
||||||
|
contentType = ""
|
||||||
} else {
|
} else {
|
||||||
req.Header.Set(HeaderKeyContentType, body.ContentType)
|
contentType = wf.Body.ContentType
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if contentType == "" {
|
||||||
|
if wf.Method == "POST" || wf.Method == "PUT" || wf.Method == "PATCH" {
|
||||||
|
contentType = TypeURLENCODED
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if contentType != "" {
|
||||||
|
req.Header.Set(HeaderKeyContentType, contentType)
|
||||||
}
|
}
|
||||||
|
|
||||||
return req
|
return req
|
||||||
|
|||||||
91
session.go
91
session.go
@@ -7,6 +7,7 @@ import (
|
|||||||
"net/http/cookiejar"
|
"net/http/cookiejar"
|
||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/net/publicsuffix"
|
"golang.org/x/net/publicsuffix"
|
||||||
@@ -33,9 +34,11 @@ type Session struct {
|
|||||||
client *http.Client
|
client *http.Client
|
||||||
transport *http.Transport
|
transport *http.Transport
|
||||||
cookiejar http.CookieJar
|
cookiejar http.CookieJar
|
||||||
params *Body
|
body *Body
|
||||||
Header http.Header
|
|
||||||
auth *BasicAuth
|
auth *BasicAuth
|
||||||
|
|
||||||
|
Header http.Header
|
||||||
|
Query url.Values
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -45,6 +48,10 @@ const (
|
|||||||
TypeXML = "text/xml"
|
TypeXML = "text/xml"
|
||||||
// TypeURLENCODED 类型
|
// TypeURLENCODED 类型
|
||||||
TypeURLENCODED = "application/x-www-form-urlencoded"
|
TypeURLENCODED = "application/x-www-form-urlencoded"
|
||||||
|
// TypeForm PostForm类型
|
||||||
|
TypeForm = TypeURLENCODED
|
||||||
|
// TypeContentEmpty 没有Form的类型 Content
|
||||||
|
TypeContentEmpty = "ContentEmpty"
|
||||||
// TypeFormData 类型
|
// TypeFormData 类型
|
||||||
TypeFormData = "multipart/form-data"
|
TypeFormData = "multipart/form-data"
|
||||||
// HeaderKeyHost Host
|
// HeaderKeyHost Host
|
||||||
@@ -94,7 +101,7 @@ func NewSession() *Session {
|
|||||||
}
|
}
|
||||||
|
|
||||||
client.Jar = cjar
|
client.Jar = cjar
|
||||||
return &Session{client: client, params: &Body{}, transport: transport, auth: nil, cookiejar: client.Jar, Header: make(http.Header)}
|
return &Session{client: client, body: &Body{}, transport: transport, auth: nil, cookiejar: client.Jar, Header: make(http.Header)}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetConfig 设置配置
|
// SetConfig 设置配置
|
||||||
@@ -121,15 +128,11 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
|
|||||||
case ConfigCookiejar:
|
case ConfigCookiejar:
|
||||||
v := values.(bool)
|
v := values.(bool)
|
||||||
if v {
|
if v {
|
||||||
if ses.cookiejar == nil {
|
if ses.client.Jar == nil {
|
||||||
j, err := cookiejar.New(nil)
|
ses.client.Jar = ses.cookiejar
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
ses.cookiejar = j
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ses.cookiejar = nil
|
ses.client.Jar = nil
|
||||||
}
|
}
|
||||||
case ConfigProxy:
|
case ConfigProxy:
|
||||||
switch v := values.(type) {
|
switch v := values.(type) {
|
||||||
@@ -170,6 +173,26 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetQuery 设置url query的持久参数的值
|
||||||
|
func (ses *Session) SetQuery(values url.Values) {
|
||||||
|
ses.Query = values
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetQuery 获取get query的值
|
||||||
|
func (ses *Session) GetQuery() url.Values {
|
||||||
|
return ses.Query
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetHeader 设置set Header的值
|
||||||
|
func (ses *Session) SetHeader(header http.Header) {
|
||||||
|
ses.Header = header
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetHeader 获取get Header的值
|
||||||
|
func (ses *Session) GetHeader() http.Header {
|
||||||
|
return ses.Header
|
||||||
|
}
|
||||||
|
|
||||||
// SetCookies 设置Cookies 或者添加Cookies Del
|
// SetCookies 设置Cookies 或者添加Cookies Del
|
||||||
func (ses *Session) SetCookies(u *url.URL, cookies []*http.Cookie) {
|
func (ses *Session) SetCookies(u *url.URL, cookies []*http.Cookie) {
|
||||||
ses.cookiejar.SetCookies(u, cookies)
|
ses.cookiejar.SetCookies(u, cookies)
|
||||||
@@ -177,6 +200,7 @@ func (ses *Session) SetCookies(u *url.URL, cookies []*http.Cookie) {
|
|||||||
|
|
||||||
// Cookies 返回 Cookies
|
// Cookies 返回 Cookies
|
||||||
func (ses *Session) Cookies(u *url.URL) []*http.Cookie {
|
func (ses *Session) Cookies(u *url.URL) []*http.Cookie {
|
||||||
|
|
||||||
return ses.cookiejar.Cookies(u)
|
return ses.cookiejar.Cookies(u)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,58 +215,77 @@ func (ses *Session) DelCookies(u *url.URL, name string) {
|
|||||||
ses.SetCookies(u, cookies)
|
ses.SetCookies(u, cookies)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ClearCookies 清楚所有cookiejar上的cookies
|
||||||
|
func (ses *Session) ClearCookies() {
|
||||||
|
cjar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
ses.cookiejar = cjar
|
||||||
|
ses.client.Jar = ses.cookiejar
|
||||||
|
}
|
||||||
|
|
||||||
// Get 请求
|
// Get 请求
|
||||||
func (ses *Session) Get(url string) *Workflow {
|
func (ses *Session) Get(url string) *Workflow {
|
||||||
wf := NewWorkflow(ses)
|
wf := NewWorkflow(ses, url)
|
||||||
wf.Method = "GET"
|
wf.Method = "GET"
|
||||||
wf.SetURL(url)
|
|
||||||
return wf
|
return wf
|
||||||
}
|
}
|
||||||
|
|
||||||
// Post 请求
|
// Post 请求
|
||||||
func (ses *Session) Post(url string) *Workflow {
|
func (ses *Session) Post(url string) *Workflow {
|
||||||
wf := NewWorkflow(ses)
|
wf := NewWorkflow(ses, url)
|
||||||
wf.Method = "POST"
|
wf.Method = "POST"
|
||||||
wf.SetURL(url)
|
|
||||||
return wf
|
return wf
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put 请求
|
// Put 请求
|
||||||
func (ses *Session) Put(url string) *Workflow {
|
func (ses *Session) Put(url string) *Workflow {
|
||||||
wf := NewWorkflow(ses)
|
wf := NewWorkflow(ses, url)
|
||||||
wf.Method = "PUT"
|
wf.Method = "PUT"
|
||||||
wf.SetURL(url)
|
|
||||||
return wf
|
return wf
|
||||||
}
|
}
|
||||||
|
|
||||||
// Patch 请求
|
// Patch 请求
|
||||||
func (ses *Session) Patch(url string) *Workflow {
|
func (ses *Session) Patch(url string) *Workflow {
|
||||||
wf := NewWorkflow(ses)
|
wf := NewWorkflow(ses, url)
|
||||||
wf.Method = "PATCH"
|
wf.Method = "PATCH"
|
||||||
wf.SetURL(url)
|
|
||||||
return wf
|
return wf
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete 请求
|
// Delete 请求
|
||||||
func (ses *Session) Delete(url string) *Workflow {
|
func (ses *Session) Delete(url string) *Workflow {
|
||||||
wf := NewWorkflow(ses)
|
wf := NewWorkflow(ses, url)
|
||||||
wf.Method = "DELETE"
|
wf.Method = "DELETE"
|
||||||
wf.SetURL(url)
|
|
||||||
return wf
|
return wf
|
||||||
}
|
}
|
||||||
|
|
||||||
// Head 请求
|
// Head 请求
|
||||||
func (ses *Session) Head(url string) *Workflow {
|
func (ses *Session) Head(url string) *Workflow {
|
||||||
wf := NewWorkflow(ses)
|
wf := NewWorkflow(ses, url)
|
||||||
wf.Method = "HEAD"
|
wf.Method = "HEAD"
|
||||||
wf.SetURL(url)
|
|
||||||
return wf
|
return wf
|
||||||
}
|
}
|
||||||
|
|
||||||
// Options 请求
|
// Options 请求
|
||||||
func (ses *Session) Options(url string) *Workflow {
|
func (ses *Session) Options(url string) *Workflow {
|
||||||
wf := NewWorkflow(ses)
|
wf := NewWorkflow(ses, url)
|
||||||
wf.Method = "OPTIONS"
|
wf.Method = "OPTIONS"
|
||||||
wf.SetURL(url)
|
|
||||||
return wf
|
return wf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CloseIdleConnections closes the idle connections that a session client may make use of
|
||||||
|
// 从levigross/grequests 借鉴
|
||||||
|
func (ses *Session) CloseIdleConnections() {
|
||||||
|
ses.client.Transport.(*http.Transport).CloseIdleConnections()
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnsureTransporterFinalized will ensure that when the HTTP client is GCed
|
||||||
|
// the runtime will close the idle connections (so that they won't leak)
|
||||||
|
// this function was adopted from Hashicorp's go-cleanhttp package
|
||||||
|
// 暂时不用, 标记到以后是否起作用
|
||||||
|
func EnsureTransporterFinalized(httpTransport *http.Transport) {
|
||||||
|
runtime.SetFinalizer(&httpTransport, func(transportInt **http.Transport) {
|
||||||
|
(*transportInt).CloseIdleConnections()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -30,7 +30,6 @@ func TestSession_Get(t *testing.T) {
|
|||||||
fields fields
|
fields fields
|
||||||
args args
|
args args
|
||||||
}{
|
}{
|
||||||
// TODO: Add test cases.
|
|
||||||
{
|
{
|
||||||
name: "Get test",
|
name: "Get test",
|
||||||
fields: fields{client: &http.Client{}},
|
fields: fields{client: &http.Client{}},
|
||||||
@@ -107,7 +106,6 @@ func TestSession_Setparams(t *testing.T) {
|
|||||||
want *regexp.Regexp
|
want *regexp.Regexp
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
// TODO: Add test cases.
|
|
||||||
{
|
{
|
||||||
name: "test Setparams",
|
name: "test Setparams",
|
||||||
args: args{params: []interface{}{map[string]string{"a": "1", "b": "2"}}},
|
args: args{params: []interface{}{map[string]string{"a": "1", "b": "2"}}},
|
||||||
@@ -285,7 +283,6 @@ func TestSession_SetConfig(t *testing.T) {
|
|||||||
args args
|
args args
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
// TODO: Add test cases.
|
|
||||||
{
|
{
|
||||||
name: "test timeout",
|
name: "test timeout",
|
||||||
args: args{typeConfig: ConfigRequestTimeout, values: 0.01},
|
args: args{typeConfig: ConfigRequestTimeout, values: 0.01},
|
||||||
@@ -343,6 +340,8 @@ func TestSession_SetConfigInsecure(t *testing.T) {
|
|||||||
func TestSession_Cookies(t *testing.T) {
|
func TestSession_Cookies(t *testing.T) {
|
||||||
ses := NewSession()
|
ses := NewSession()
|
||||||
|
|
||||||
|
t.Run("set cookie", func(t *testing.T) {
|
||||||
|
|
||||||
resp, err := ses.Get("http://httpbin.org/cookies/set").AddKVCookie("a", "1").Execute()
|
resp, err := ses.Get("http://httpbin.org/cookies/set").AddKVCookie("a", "1").Execute()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("cookies set error", err)
|
t.Error("cookies set error", err)
|
||||||
@@ -351,12 +350,15 @@ func TestSession_Cookies(t *testing.T) {
|
|||||||
if !regexp.MustCompile(`"a": "1"`).MatchString(resp.DContent) {
|
if !regexp.MustCompile(`"a": "1"`).MatchString(resp.DContent) {
|
||||||
t.Error(resp.DContent)
|
t.Error(resp.DContent)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSession_Header(t *testing.T) {
|
func TestSession_Header(t *testing.T) {
|
||||||
chromeua := "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
|
chromeua := "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
|
||||||
ses := NewSession()
|
ses := NewSession()
|
||||||
|
|
||||||
|
t.Run("ua header test", func(t *testing.T) {
|
||||||
|
|
||||||
ses.Header.Add(HeaderKeyUA, chromeua)
|
ses.Header.Add(HeaderKeyUA, chromeua)
|
||||||
resp, err := ses.Get("https://www.baidu.com").Execute()
|
resp, err := ses.Get("https://www.baidu.com").Execute()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -376,4 +378,5 @@ func TestSession_Header(t *testing.T) {
|
|||||||
if len(resp.DContent) <= 5000 {
|
if len(resp.DContent) <= 5000 {
|
||||||
t.Error(resp.DContent, len(resp.DContent))
|
t.Error(resp.DContent, len(resp.DContent))
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
84
workflow.go
84
workflow.go
@@ -1,9 +1,11 @@
|
|||||||
package requests
|
package requests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Workflow 工作流
|
// Workflow 工作流
|
||||||
@@ -17,10 +19,12 @@ type Workflow struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewWorkflow new and init workflow
|
// NewWorkflow new and init workflow
|
||||||
func NewWorkflow(ses *Session) *Workflow {
|
func NewWorkflow(ses *Session, u string) *Workflow {
|
||||||
wf := &Workflow{}
|
wf := &Workflow{}
|
||||||
wf.SwitchSession(ses)
|
wf.SwitchSession(ses)
|
||||||
|
|
||||||
|
wf.SetURL(u)
|
||||||
|
|
||||||
wf.Body = &Body{}
|
wf.Body = &Body{}
|
||||||
wf.Header = make(http.Header)
|
wf.Header = make(http.Header)
|
||||||
wf.Cookies = make(map[string]*http.Cookie)
|
wf.Cookies = make(map[string]*http.Cookie)
|
||||||
@@ -44,6 +48,16 @@ func (wf *Workflow) SetHeader(key, value string) *Workflow {
|
|||||||
return wf
|
return wf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetHeader 获取Workflow Header
|
||||||
|
func (wf *Workflow) GetHeader() http.Header {
|
||||||
|
return wf.Header
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCombineHeader 获取后的Header信息
|
||||||
|
func (wf *Workflow) GetCombineHeader() http.Header {
|
||||||
|
return mergeMapList(wf.session.Header, wf.Header)
|
||||||
|
}
|
||||||
|
|
||||||
// DelHeader 添加头信息 Get方法从Header参数上获取
|
// DelHeader 添加头信息 Get方法从Header参数上获取
|
||||||
func (wf *Workflow) DelHeader(key string) *Workflow {
|
func (wf *Workflow) DelHeader(key string) *Workflow {
|
||||||
wf.Header.Del(key)
|
wf.Header.Del(key)
|
||||||
@@ -83,7 +97,8 @@ func (wf *Workflow) DelCookie(name interface{}) *Workflow {
|
|||||||
|
|
||||||
// GetStringURL 获取url的string形式
|
// GetStringURL 获取url的string形式
|
||||||
func (wf *Workflow) GetStringURL() string {
|
func (wf *Workflow) GetStringURL() string {
|
||||||
return wf.ParsedURL.String()
|
u := strings.Split(wf.ParsedURL.String(), "?")[0] + "?" + wf.GetCombineQuery().Encode()
|
||||||
|
return u
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetURL 设置 url
|
// SetURL 设置 url
|
||||||
@@ -96,20 +111,26 @@ func (wf *Workflow) SetURL(srcURL string) *Workflow {
|
|||||||
return wf
|
return wf
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetURLQuery 获取Query参数
|
// GetQuery 获取Query参数
|
||||||
func (wf *Workflow) GetURLQuery() url.Values {
|
func (wf *Workflow) GetQuery() url.Values {
|
||||||
if wf.ParsedURL != nil {
|
|
||||||
return wf.ParsedURL.Query()
|
return wf.ParsedURL.Query()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetCombineQuery 获取Query参数
|
||||||
|
func (wf *Workflow) GetCombineQuery() url.Values {
|
||||||
|
if wf.ParsedURL != nil {
|
||||||
|
vs := wf.ParsedURL.Query()
|
||||||
|
return mergeMapList(wf.session.GetQuery(), vs)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetURLQuery 设置Query参数
|
// SetQuery 设置Query参数
|
||||||
func (wf *Workflow) SetURLQuery(query url.Values) *Workflow {
|
func (wf *Workflow) SetQuery(query url.Values) *Workflow {
|
||||||
if query == nil {
|
if query == nil {
|
||||||
return wf
|
return wf
|
||||||
}
|
}
|
||||||
|
query = (url.Values)(mergeMapList(wf.session.Query, query))
|
||||||
wf.ParsedURL.RawQuery = query.Encode()
|
wf.ParsedURL.RawQuery = query.Encode()
|
||||||
return wf
|
return wf
|
||||||
}
|
}
|
||||||
@@ -147,7 +168,7 @@ func (wf *Workflow) SetURLPath(path []string) *Workflow {
|
|||||||
return wf
|
return wf
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetURLRawPath 设置Path参数
|
// SetURLRawPath 设置Pa晚上参数
|
||||||
func (wf *Workflow) SetURLRawPath(path string) *Workflow {
|
func (wf *Workflow) SetURLRawPath(path string) *Workflow {
|
||||||
wf.ParsedURL.Path = path
|
wf.ParsedURL.Path = path
|
||||||
return wf
|
return wf
|
||||||
@@ -155,9 +176,7 @@ func (wf *Workflow) SetURLRawPath(path string) *Workflow {
|
|||||||
|
|
||||||
// SetBodyParams 参数设置
|
// SetBodyParams 参数设置
|
||||||
func (wf *Workflow) SetBodyParams(params ...interface{}) *Workflow {
|
func (wf *Workflow) SetBodyParams(params ...interface{}) *Workflow {
|
||||||
if params == nil {
|
if params != nil {
|
||||||
return wf
|
|
||||||
}
|
|
||||||
|
|
||||||
plen := len(params)
|
plen := len(params)
|
||||||
defaultContentType := TypeURLENCODED
|
defaultContentType := TypeURLENCODED
|
||||||
@@ -171,9 +190,9 @@ func (wf *Workflow) SetBodyParams(params ...interface{}) *Workflow {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if defaultContentType == TypeFormData {
|
if defaultContentType == TypeFormData {
|
||||||
// TODO: form-data
|
|
||||||
createMultipart(wf.Body, params)
|
createMultipart(wf.Body, params)
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
var values url.Values
|
var values url.Values
|
||||||
switch param := params[0].(type) {
|
switch param := params[0].(type) {
|
||||||
case map[string]string:
|
case map[string]string:
|
||||||
@@ -190,14 +209,20 @@ func (wf *Workflow) SetBodyParams(params ...interface{}) *Workflow {
|
|||||||
case []byte:
|
case []byte:
|
||||||
wf.Body.IOBody = param
|
wf.Body.IOBody = param
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return wf
|
return wf
|
||||||
}
|
}
|
||||||
|
|
||||||
// setHeaderRequest 设置request的头
|
func mergeMapList(headers ...map[string][]string) map[string][]string {
|
||||||
func setHeaderRequest(req *http.Request, wf *Workflow) {
|
|
||||||
set := make(map[string]map[string]int)
|
set := make(map[string]map[string]int)
|
||||||
for key, values := range wf.session.Header {
|
merged := make(map[string][]string)
|
||||||
|
|
||||||
|
for _, header := range headers {
|
||||||
|
for key, values := range header {
|
||||||
for _, v := range values {
|
for _, v := range values {
|
||||||
if vs, ok := set[key]; ok {
|
if vs, ok := set[key]; ok {
|
||||||
vs[v] = 1
|
vs[v] = 1
|
||||||
@@ -207,23 +232,25 @@ func setHeaderRequest(req *http.Request, wf *Workflow) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for key, values := range wf.Header {
|
|
||||||
for _, v := range values {
|
|
||||||
if vs, ok := set[key]; ok {
|
|
||||||
vs[v] = 1
|
|
||||||
} else {
|
|
||||||
set[key] = make(map[string]int)
|
|
||||||
set[key][v] = 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, mvalue := range set {
|
for key, mvalue := range set {
|
||||||
for v := range mvalue {
|
for v := range mvalue {
|
||||||
req.Header.Add(key, v)
|
// merged.Add(key, v)
|
||||||
|
if mergeValue, ok := merged[key]; ok {
|
||||||
|
merged[key] = append(mergeValue, v)
|
||||||
|
} else {
|
||||||
|
merged[key] = []string{v}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return merged
|
||||||
|
}
|
||||||
|
|
||||||
|
// setHeaderRequest 设置request的头
|
||||||
|
func setHeaderRequest(req *http.Request, wf *Workflow) {
|
||||||
|
req.Header = mergeMapList(req.Header, wf.session.Header, wf.Header)
|
||||||
}
|
}
|
||||||
|
|
||||||
// setHeaderRequest 设置request的临时Cookie, 永久需要在session上设置cookie
|
// setHeaderRequest 设置request的临时Cookie, 永久需要在session上设置cookie
|
||||||
@@ -238,11 +265,16 @@ func setTempCookieRequest(req *http.Request, wf *Workflow) {
|
|||||||
// Execute 执行
|
// Execute 执行
|
||||||
func (wf *Workflow) Execute() (*Response, error) {
|
func (wf *Workflow) Execute() (*Response, error) {
|
||||||
|
|
||||||
req := buildBodyRequest(wf.Method, wf.GetStringURL(), wf.Body)
|
req := buildBodyRequest(wf)
|
||||||
|
|
||||||
setHeaderRequest(req, wf)
|
setHeaderRequest(req, wf)
|
||||||
setTempCookieRequest(req, wf)
|
setTempCookieRequest(req, wf)
|
||||||
|
|
||||||
|
if wf.session.auth != nil {
|
||||||
|
req.SetBasicAuth(wf.session.auth.User, wf.session.auth.Password)
|
||||||
|
}
|
||||||
|
log.Println(req.Header)
|
||||||
|
|
||||||
resp, err := wf.session.client.Do(req)
|
resp, err := wf.session.client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
Reference in New Issue
Block a user