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