-
Notifications
You must be signed in to change notification settings - Fork 4
/
example_test.go
70 lines (56 loc) · 1.5 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
package requests_test
import (
"fmt"
"log"
"strings"
"github.com/pkg/requests"
)
func ExampleClient_Get() {
var c requests.Client
resp, err := c.Get("https://www.example.com")
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.Request.Method, resp.Request.URL, resp.Status.Code)
}
func ExampleClient_Post() {
var c requests.Client
body := strings.NewReader("Hello there!")
resp, err := c.Post("https://www.example.com", body, requests.WithHeader("Content-Type", "application/x-www-form-urlencoded"))
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.Request.Method, resp.Request.URL, resp.Status.Code)
}
func ExampleBody_JSON() {
var c requests.Client
resp, err := c.Get("https://frinkiac.com/api/search?q=burn+that+seat")
if err != nil {
log.Fatal(err)
}
defer resp.Close()
if !resp.IsSuccess() {
log.Fatalf("%s: expected 200, got %v", resp.Request.URL, resp.Status)
}
var results []struct {
Id int `json:"Id"`
Episode string `json:"Episode"`
Timestamp int `json:"Timestamp"`
}
err = resp.JSON(&results)
fmt.Printf("%#v\n%v", results, err)
}
var response = requests.Response{
Headers: []requests.Header{
{Key: "Server", Values: []string{"nginx/1.2.1"}},
{Key: "Connection", Values: []string{"keep-alive"}},
{Key: "Content-Type", Values: []string{"text/html; charset=UTF-8"}},
},
}
func ExampleResponse_Header() {
fmt.Println(response.Header("Server"))
fmt.Println(response.Header("Content-Type"))
// Output:
// nginx/1.2.1
// text/html; charset=UTF-8
}