-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexample_test.go
87 lines (74 loc) · 1.59 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package request_test
import (
"net/http"
"net/url"
"time"
"github.com/DavidCai1993/request"
)
var (
text string
err error
res *request.Response
json interface{}
)
func Example() {
res, err = request.
Get("http://mysite.com").
End()
// json has the type *simplejson.Json
json, err = request.
Post("http://mysite.com").
Timeout(30*time.Second).
Send(map[string]string{"name": "David"}).
Set("X-HEADER-KEY", "foo").
Accept("application/json").
JSON()
}
func ExampleGet() {
json, err = request.
Get("http://mysite.com").
JSON()
}
func ExampleGetWithStruct() {
type MyResult struct {
Code int `json:"code"`
Error string `json:"error"`
Data map[string]interface{} `json:"data"`
}
json, err = request.
Get("http://mysite.com").
JSON(new(MyResult))
}
func ExamplePost() {
json, err = request.
Post("http://mysite.com").
Send(map[string]string{"name": "David"}).
Set("X-HEADER-KEY", "foo").
Accept("application/json").
JSON()
}
func ExampleClient_Cookie() {
text, err = request.
Get("http://mysite.com/get").
Cookie(&http.Cookie{Name: "name", Value: "David"}).
Text()
}
func ExampleClient_Auth() {
json, err = request.
Get("http://mysite.com/somebooks").
Auth("name", "passwd").
JSON()
}
func ExampleClient_Proxy() {
json, err = request.
Get("http://mysite.com/somebooks").
Proxy("http://myproxy.com:8080").
JSON()
}
func ExampleClient_Attach() {
json, err = request.
Post("http://mysite.com/readme").
Field(url.Values{"key": []string{"value1"}}).
Attach("test.md", "./README.md", "README.md").
JSON()
}