Skip to content

Commit ba6667e

Browse files
committed
improve api params
1 parent fd42f1a commit ba6667e

File tree

5 files changed

+104
-77
lines changed

5 files changed

+104
-77
lines changed

README.md

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,29 +63,43 @@ default request headers of this request. They are shared between different HTTP
6363
requests.
6464

6565

66-
### GET/POST
67-
68-
In most cases you just need the `Get` and `Post` method after initializing:
66+
### Sending Request
6967

7068
```go
71-
func (this *HttpClient) Get(url string, params ...map[string]string) (*httpclient.Response, error)
69+
// get
70+
httpclient.Get("http://httpbin.org/get", map[string]string{
71+
"q": "news",
72+
})
7273

73-
func (this *HttpClient) Post(url string, params map[string]string) (*httpclient.Response, error)
74-
```
74+
// get with url.Values
75+
httpclient.Get("http://httpbin.org/get", url.Values{
76+
"q": []string{"news", "today"}
77+
})
7578

76-
```go
77-
res, err := httpclient.Get("http://google.com/search", map[string]string{
78-
"q": "news",
79-
})
79+
// post
80+
httpclient.Post("http://httpbin.org/post", map[string]string {
81+
"name": "value"
82+
})
83+
84+
// post file(multipart)
85+
httpclient.Post("http://httpbin.org/multipart", map[string]string {
86+
"@file": "/tmp/hello.pdf",
87+
})
8088

81-
fmt.Println(res.StatusCode, err)
89+
// put json
90+
httpclient.PutJson("http://httpbin.org/put",
91+
`{
92+
"name": "hello",
93+
}`)
8294

83-
// post file
84-
res, err := httpclient.Post("http://dropbox.com/upload", map[string]string {
85-
"@file": "/tmp/hello.pdf",
86-
})
95+
// delete
96+
httpclient.Delete("http://httpbin.org/delete")
97+
98+
// options
99+
httpclient.Options("http://httpbin.org")
87100

88-
fmt.Println(res, err)
101+
// head
102+
httpclient.Head("http://httpbin.org/get")
89103
```
90104

91105
### Customize Request
@@ -106,7 +120,7 @@ httpclient.
106120
Name: "uid",
107121
Value: "123",
108122
}).
109-
Get("http://github.com", nil)
123+
Get("http://github.com")
110124
```
111125

112126
### Response
@@ -115,16 +129,16 @@ The `httpclient.Response` is a thin wrap of `http.Response`.
115129

116130
```go
117131
// traditional
118-
res, err := httpclient.Get("http://google.com", nil)
132+
res, err := httpclient.Get("http://google.com")
119133
bodyBytes, err := ioutil.ReadAll(res.Body)
120134
res.Body.Close()
121135

122136
// ToString
123-
res, err = httpclient.Get("http://google.com", nil)
137+
res, err = httpclient.Get("http://google.com")
124138
bodyString,err := res.ToString()
125139

126140
// ReadAll
127-
res, err = httpclient.Get("http://google.com", nil)
141+
res, err = httpclient.Get("http://google.com")
128142
bodyBytes := res.ReadAll()
129143
```
130144

@@ -137,7 +151,7 @@ httpclient.
137151
Name: "uid",
138152
Value: "123",
139153
}).
140-
Get(url, nil)
154+
Get(url)
141155

142156
for _, cookie := range httpclient.Cookies() {
143157
fmt.Println(cookie.Name, cookie.Value)
@@ -176,7 +190,7 @@ go func() {
176190
You can use `httpclient.IsTimeoutError` to check for timeout error:
177191

178192
```go
179-
res, err := httpclient.Get("http://google.com", nil)
193+
res, err := httpclient.Get("http://google.com")
180194
if httpclient.IsTimeoutError(err) {
181195
// do something
182196
}

default_client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestDefaultClient(t *testing.T) {
11-
res, err := Get("http://httpbin.org/get", nil)
11+
res, err := Get("http://httpbin.org/get")
1212

1313
if err != nil {
1414
t.Error("get failed", err)

httpclient.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -621,18 +621,18 @@ func (this *HttpClient) Head(url string) (*Response, error) {
621621
}
622622

623623
// The GET request
624-
func (this *HttpClient) Get(url string, params ...map[string]string) (*Response, error) {
624+
func (this *HttpClient) Get(url string, params ...interface{}) (*Response, error) {
625625
for _, p := range params {
626-
url = addParams(url, p)
626+
url = addParams(url, toUrlValues(p))
627627
}
628628

629629
return this.Do("GET", url, nil, nil)
630630
}
631631

632632
// The DELETE request
633-
func (this *HttpClient) Delete(url string, params ...map[string]string) (*Response, error) {
633+
func (this *HttpClient) Delete(url string, params ...interface{}) (*Response, error) {
634634
for _, p := range params {
635-
url = addParams(url, p)
635+
url = addParams(url, toUrlValues(p))
636636
}
637637

638638
return this.Do("DELETE", url, nil, nil)
@@ -645,36 +645,40 @@ func (this *HttpClient) Delete(url string, params ...map[string]string) (*Respon
645645
//
646646
// If any of the params key starts with "@", it is considered as a form file
647647
// (similar to CURL but different).
648-
func (this *HttpClient) Post(url string, params map[string]string) (*Response,
648+
func (this *HttpClient) Post(url string, params interface{}) (*Response,
649649
error) {
650+
paramsValues := toUrlValues(params)
650651
// Post with files should be sent as multipart.
651-
if checkParamFile(params) {
652+
if checkParamFile(paramsValues) {
652653
return this.PostMultipart(url, params)
653654
}
654655

655656
headers := make(map[string]string)
656657
headers["Content-Type"] = "application/x-www-form-urlencoded"
657-
body := strings.NewReader(paramsToString(params))
658+
body := strings.NewReader(paramsValues.Encode())
658659

659660
return this.Do("POST", url, headers, body)
660661
}
661662

662663
// Post with the request encoded as "multipart/form-data".
663-
func (this *HttpClient) PostMultipart(url string, params map[string]string) (
664+
func (this *HttpClient) PostMultipart(url string, params interface{}) (
664665
*Response, error) {
665666
body := &bytes.Buffer{}
666667
writer := multipart.NewWriter(body)
667668

669+
paramsValues := toUrlValues(params)
668670
// check files
669-
for k, v := range params {
670-
// is file
671-
if k[0] == '@' {
672-
err := addFormFile(writer, k[1:], v)
673-
if err != nil {
674-
return nil, err
671+
for k, v := range paramsValues {
672+
for _, vv := range v {
673+
// is file
674+
if k[0] == '@' {
675+
err := addFormFile(writer, k[1:], vv)
676+
if err != nil {
677+
return nil, err
678+
}
679+
} else {
680+
writer.WriteField(k, vv)
675681
}
676-
} else {
677-
writer.WriteField(k, v)
678682
}
679683
}
680684
headers := make(map[string]string)
@@ -726,7 +730,7 @@ func (this *HttpClient) PutJson(url string, data interface{}) (*Response, error)
726730
// The OPTIONS request
727731
func (this *HttpClient) Options(url string, params ...map[string]string) (*Response, error) {
728732
for _, p := range params {
729-
url = addParams(url, p)
733+
url = addParams(url, toUrlValues(p))
730734
}
731735

732736
return this.Do("OPTIONS", url, nil, nil)
@@ -735,7 +739,7 @@ func (this *HttpClient) Options(url string, params ...map[string]string) (*Respo
735739
// The CONNECT request
736740
func (this *HttpClient) Connect(url string, params ...map[string]string) (*Response, error) {
737741
for _, p := range params {
738-
url = addParams(url, p)
742+
url = addParams(url, toUrlValues(p))
739743
}
740744

741745
return this.Do("CONNECT", url, nil, nil)
@@ -744,7 +748,7 @@ func (this *HttpClient) Connect(url string, params ...map[string]string) (*Respo
744748
// The TRACE request
745749
func (this *HttpClient) Trace(url string, params ...map[string]string) (*Response, error) {
746750
for _, p := range params {
747-
url = addParams(url, p)
751+
url = addParams(url, toUrlValues(p))
748752
}
749753

750754
return this.Do("TRACE", url, nil, nil)
@@ -753,7 +757,7 @@ func (this *HttpClient) Trace(url string, params ...map[string]string) (*Respons
753757
// The PATCH request
754758
func (this *HttpClient) Patch(url string, params ...map[string]string) (*Response, error) {
755759
for _, p := range params {
756-
url = addParams(url, p)
760+
url = addParams(url, toUrlValues(p))
757761
}
758762

759763
return this.Do("PATCH", url, nil, nil)

0 commit comments

Comments
 (0)