-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
59 lines (55 loc) · 1.18 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
package gohttp
import (
"fmt"
"log"
"net/url"
"strings"
)
func Example() {
resp, err := Post("https://httpbin.org/post", nil, url.Values{"hello": []string{"world"}})
if err != nil {
log.Fatal(err)
}
var data struct {
Form struct{ Hello string }
}
if err := resp.JSON(&data); err != nil {
log.Fatal(err)
}
fmt.Println(data.Form.Hello)
// world
}
func ExampleUpload() {
resp, err := Upload("https://httpbin.org/post", nil, nil, F("readme", "README.md"))
if err != nil {
log.Fatal(err)
}
var data struct {
Files struct{ Readme string }
Headers struct {
ContentType string `json:"Content-Type"`
}
}
if err := resp.JSON(&data); err != nil {
log.Fatal(err)
}
fmt.Println(strings.Split(data.Headers.ContentType, ";")[0])
// multipart/form-data
}
func ExampleSession() {
s := NewSession()
s.Header.Set("hello", "world")
s.Get("https://httpbin.org/cookies/set/name/value", nil)
resp, err := s.Get("https://httpbin.org/get", nil)
if err != nil {
log.Fatal(err)
}
var data struct {
Headers struct{ Hello, Cookie string }
}
if err := resp.JSON(&data); err != nil {
log.Fatal(err)
}
fmt.Println(data.Headers.Hello, data.Headers.Cookie)
// world name=value
}