mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 18:30:56 +00:00
优化设置参数
This commit is contained in:
parent
ff92f22d84
commit
862ea226e5
@ -100,7 +100,7 @@ func NewBeegoRequest(rawurl, method string) *BeegoHttpRequest {
|
|||||||
return &BeegoHttpRequest{
|
return &BeegoHttpRequest{
|
||||||
url: rawurl,
|
url: rawurl,
|
||||||
req: &req,
|
req: &req,
|
||||||
params: map[string]interface{}{},
|
params: map[string][]string{},
|
||||||
files: map[string]string{},
|
files: map[string]string{},
|
||||||
setting: defaultSetting,
|
setting: defaultSetting,
|
||||||
resp: &resp,
|
resp: &resp,
|
||||||
@ -150,7 +150,7 @@ type BeegoHttpSettings struct {
|
|||||||
type BeegoHttpRequest struct {
|
type BeegoHttpRequest struct {
|
||||||
url string
|
url string
|
||||||
req *http.Request
|
req *http.Request
|
||||||
params map[string]interface{}
|
params map[string][]string
|
||||||
files map[string]string
|
files map[string]string
|
||||||
setting BeegoHttpSettings
|
setting BeegoHttpSettings
|
||||||
resp *http.Response
|
resp *http.Response
|
||||||
@ -272,8 +272,12 @@ func (b *BeegoHttpRequest) SetProxy(proxy func(*http.Request) (*url.URL, error))
|
|||||||
|
|
||||||
// Param adds query param in to request.
|
// Param adds query param in to request.
|
||||||
// params build query string as ?key1=value1&key2=value2...
|
// params build query string as ?key1=value1&key2=value2...
|
||||||
func (b *BeegoHttpRequest) Param(key string, value interface{}) *BeegoHttpRequest {
|
func (b *BeegoHttpRequest) Param(key, value string) *BeegoHttpRequest {
|
||||||
b.params[key] = value
|
if param, ok := b.params[key]; ok {
|
||||||
|
b.params[key] = append(param, value)
|
||||||
|
} else {
|
||||||
|
b.params[key] = []string{value}
|
||||||
|
}
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -348,15 +352,10 @@ func (b *BeegoHttpRequest) buildUrl(paramBody string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for k, v := range b.params {
|
for k, v := range b.params {
|
||||||
switch v.(type) {
|
for _, vv := range v {
|
||||||
case string:
|
|
||||||
bodyWriter.WriteField(k, v.(string))
|
|
||||||
case []string:
|
|
||||||
for _, vv := range v.([]string) {
|
|
||||||
bodyWriter.WriteField(k, vv)
|
bodyWriter.WriteField(k, vv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
bodyWriter.Close()
|
bodyWriter.Close()
|
||||||
pw.Close()
|
pw.Close()
|
||||||
}()
|
}()
|
||||||
@ -390,21 +389,13 @@ func (b *BeegoHttpRequest) SendOut() (*http.Response, error) {
|
|||||||
if len(b.params) > 0 {
|
if len(b.params) > 0 {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
for k, v := range b.params {
|
for k, v := range b.params {
|
||||||
switch v.(type) {
|
for _, vv := range v {
|
||||||
case string:
|
|
||||||
buf.WriteString(url.QueryEscape(k))
|
|
||||||
buf.WriteByte('=')
|
|
||||||
buf.WriteString(url.QueryEscape(v.(string)))
|
|
||||||
buf.WriteByte('&')
|
|
||||||
case []string:
|
|
||||||
for _, vv := range v.([]string) {
|
|
||||||
buf.WriteString(url.QueryEscape(k))
|
buf.WriteString(url.QueryEscape(k))
|
||||||
buf.WriteByte('=')
|
buf.WriteByte('=')
|
||||||
buf.WriteString(url.QueryEscape(vv))
|
buf.WriteString(url.QueryEscape(vv))
|
||||||
buf.WriteByte('&')
|
buf.WriteByte('&')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
paramBody = buf.String()
|
paramBody = buf.String()
|
||||||
paramBody = paramBody[0 : len(paramBody)-1]
|
paramBody = paramBody[0 : len(paramBody)-1]
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user