-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.go
77 lines (55 loc) · 1.68 KB
/
doc.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
/*
RestReq is a wrapper around standard Go net/http client. In a simple call you can use json encoding, add headers
and parse result. This should be sufficient in most use cases.
# Examples
- Simplest use
resp, err := restreq.New("http://example.com").Post()
- You can add a header
resp, err := restreq.New("http://example.com").
AddHeader("X-TOKEN", authToken).
Post()
- Use map with JSON payload
p := map[string]any{
"string": "string",
"bool": true,
"float": 2.34,
}
resp, err := restreq.New("http://example.com").
SetContentTypeJSON().
SetJSONPayload(p).
Post()
- JSON payload with KV
resp, err := restreq.New("http://example.com").
SetContentTypeJSON().
SetUserAgent("Client 1.0").
AddJSONKeyValue("string", "string").
AddJSONKeyValue("bool", true).
AddJSONKeyValue("float", 2.34).
Post()
# Parsing response
- In the default behavior, the body of the response is copied to Response.Body, and you don't have to
call http.Response.Body.Close()
resp, err := restreq.New("http://example.com").Post()
if err == nil {
fmt.Printf("%s\n", resp.Body)
}
- Default behavior is convenient but not optimal, due to redundant copying. If you need high performance,
you can disable this behavior and direct access to the io.Reader. Don't forget to call Response.Body.Close()
resp, err := restreq.New("http://example.com").
WithBodyReader().
Post()
if err == nil {
defer resp.Response.Body.Close()
b := bytes.NewBuffer([]byte{})
b.ReadFrom(resp.Response.Body)
fmt.Println(b.String())
}
- Get header value
value := resp.Header("token")
- Decode JSON
s := struct {
Message string `json:"message,omitempty"`
}{}
err := resp.DecodeJSON(&s)
*/
package restreq