From fd644fb2a9b7d71c1981b2e18d522bcb7b170c6a Mon Sep 17 00:00:00 2001 From: john Date: Wed, 26 Sep 2018 18:44:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dghttp.BindParams=E5=AF=B9@fil?= =?UTF-8?q?e=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0=E6=A0=87=E8=AF=86?= =?UTF-8?q?=E7=AC=A6=E7=9A=84=E8=BD=AC=E4=B9=89=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/net/ghttp/ghttp_client_request.go | 11 +++++++++-- g/net/ghttp/ghttp_func.go | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/g/net/ghttp/ghttp_client_request.go b/g/net/ghttp/ghttp_client_request.go index 1108587cc..a1330b9a8 100644 --- a/g/net/ghttp/ghttp_client_request.go +++ b/g/net/ghttp/ghttp_client_request.go @@ -15,6 +15,9 @@ import ( "mime/multipart" "os" "io" + "gitee.com/johng/gf/g/os/gfile" + "errors" + "fmt" ) // http客户端 @@ -74,8 +77,12 @@ func (c *Client) Post(url, data string) (*ClientResponse, error) { for _, item := range strings.Split(data, "&") { array := strings.Split(item, "=") if len(array[1]) > 6 && strings.Compare(array[1][0:6], "@file:") == 0 { - if file, err := writer.CreateFormFile(array[0], array[1][6:]); err == nil { - if f, err := os.Open(array[1][6:]); err == nil { + path := array[1][6:] + if !gfile.Exists(path) { + return nil, errors.New(fmt.Sprintf(`"%s" does not exist`, path)) + } + if file, err := writer.CreateFormFile(array[0], path); err == nil { + if f, err := os.Open(path); err == nil { defer f.Close() if _, err = io.Copy(file, f); err != nil { return nil, err diff --git a/g/net/ghttp/ghttp_func.go b/g/net/ghttp/ghttp_func.go index 6fa79afba..74e28bcaf 100644 --- a/g/net/ghttp/ghttp_func.go +++ b/g/net/ghttp/ghttp_func.go @@ -6,7 +6,10 @@ package ghttp -import "gitee.com/johng/gf/g/encoding/gurl" +import ( + "gitee.com/johng/gf/g/encoding/gurl" + "strings" +) // 构建请求参数,将参数进行urlencode编码 func BuildParams(params map[string]string) string { @@ -15,7 +18,11 @@ func BuildParams(params map[string]string) string { if len(s) > 0 { s += "&" } - s += k + "=" + gurl.Encode(v) + if len(v) > 6 && strings.Compare(v[0 : 6], "@file:") == 0 { + s += k + "=" + v + } else { + s += k + "=" + gurl.Encode(v) + } } return s } \ No newline at end of file