From d382505453ecb91fb26a16f4603b46136ea5dddf Mon Sep 17 00:00:00 2001 From: huangsimin Date: Mon, 19 Nov 2018 18:42:31 +0800 Subject: [PATCH] test all success! --- .gitignore | 1 + session.go | 29 +++++++++++++++++++++++++---- workflow.go | 10 +++++----- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 0d20b64..294be7a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.pyc +*.vscode diff --git a/session.go b/session.go index 546f969..800c1a8 100644 --- a/session.go +++ b/session.go @@ -18,6 +18,8 @@ import ( type Body struct { // Query map[string][]string ioBody interface{} + // prefix ContentType 前缀 + prefix string // Files []UploadFile contentTypes map[string]int } @@ -41,19 +43,36 @@ func (body *Body) IOBody() interface{} { // ContentType 获取ContentType func (body *Body) ContentType() string { - content := "" + content := body.prefix + ";" for kvalue := range body.contentTypes { content += kvalue + ";" } return strings.TrimRight(content, ";") } +// AddPrefix AddPrefix 和 AddContentType的顺序会影响到ContentType()的返回结果 +func (body *Body) AddPrefix(ct string) { + content := body.prefix + for _, v := range strings.Split(ct, ";") { + v = strings.Trim(v, " ") + if v != "" { + if body.prefix != v { + content += v + ";" + } + } + } + content = strings.TrimRight(content, ";") + body.prefix = content +} + // AddContentType 添加 Add Type类型 func (body *Body) AddContentType(ct string) { for _, v := range strings.Split(ct, ";") { v = strings.Trim(v, " ") if v != "" { - body.contentTypes[v] = 1 + if body.prefix != v { + body.contentTypes[v] = 1 + } } } @@ -70,6 +89,8 @@ type IBody interface { ContentType() string // AppendContent AddContentType(ct string) + // AddPrefix 添加 Prefix + AddPrefix(ct string) } // BasicAuth 帐号认真结构 @@ -105,10 +126,10 @@ const ( TypeURLENCODED = "application/x-www-form-urlencoded" // TypeForm PostForm类型 TypeForm = TypeURLENCODED - // TypeContentEmpty 没有Form的类型 Content - TypeContentEmpty = "ContentEmpty" // TypeFormData 类型 TypeFormData = "multipart/form-data" + // TypeMixed Mixed类型 + TypeMixed = "multipart/mixed" // HeaderKeyHost Host HeaderKeyHost = "Host" diff --git a/workflow.go b/workflow.go index bd5da11..bf3235a 100644 --- a/workflow.go +++ b/workflow.go @@ -1,6 +1,7 @@ package requests import ( + "log" "net/http" "net/url" "regexp" @@ -175,23 +176,22 @@ func (wf *Workflow) SetURLRawPath(path string) *Workflow { // SetBody 参数设置 func (wf *Workflow) SetBody(params ...interface{}) *Workflow { - if params != nil { + if params != nil { plen := len(params) defaultContentType := TypeURLENCODED if plen >= 2 { t := params[plen-1] defaultContentType = t.(string) - wf.Body.AddContentType(defaultContentType) + wf.Body.AddPrefix(defaultContentType) } else { - wf.Body.AddContentType(defaultContentType) + wf.Body.AddPrefix(defaultContentType) } if defaultContentType == TypeFormData { createMultipart(wf.Body, params) } else { - var values url.Values switch param := params[0].(type) { case map[string]string: @@ -211,7 +211,7 @@ func (wf *Workflow) SetBody(params ...interface{}) *Workflow { } } - + log.Println(wf.Body.ContentType()) return wf }