improve ghttp.Client.Get

This commit is contained in:
John
2020-01-06 17:57:07 +08:00
parent 167d58490b
commit c4537b4753
2 changed files with 11 additions and 2 deletions

View File

@ -8,8 +8,8 @@ package ghttp
// Get is a convenience method for sending GET request.
// NOTE that remembers CLOSING the response object when it'll never be used.
func Get(url string) (*ClientResponse, error) {
return DoRequest("GET", url)
func Get(url string, data ...interface{}) (*ClientResponse, error) {
return DoRequest("GET", url, data...)
}
// Put is a convenience method for sending PUT request.

View File

@ -12,6 +12,7 @@ import (
"errors"
"fmt"
"github.com/gogf/gf/text/gregex"
"github.com/gogf/gf/text/gstr"
"io"
"mime/multipart"
"net/http"
@ -205,6 +206,14 @@ func (c *Client) DoRequest(method, url string, data ...interface{}) (*ClientResp
if len(data) > 0 {
param = BuildParams(data[0])
}
if strings.EqualFold("GET", method) && param != "" {
if gstr.Contains(url, "?") {
url += "&" + param
} else {
url += "?" + param
}
param = ""
}
req, err := http.NewRequest(strings.ToUpper(method), url, bytes.NewReader([]byte(param)))
if err != nil {
return nil, err