|
1 | 1 | // Request examples:
|
2 |
| -// // create a simple GET request |
3 |
| -// req := GetRequest("http://www.example.com") |
4 | 2 | //
|
5 |
| -// // set header |
6 |
| -// req.Set("Accept", "application/json") |
| 3 | +// // create a simple GET request |
| 4 | +// req := GetRequest("http://www.example.com") |
7 | 5 | //
|
8 |
| -// // set query parameters |
9 |
| -// req.Query("foo1", "bar1") |
10 |
| -// req.Query("foo2", "bar2") |
| 6 | +// // set header |
| 7 | +// req.Set("Accept", "application/json") |
11 | 8 | //
|
12 |
| -// // Build to a HTTP request |
13 |
| -// req.Build() |
| 9 | +// // set query parameters |
| 10 | +// req.Query("foo1", "bar1") |
| 11 | +// req.Query("foo2", "bar2") |
14 | 12 | //
|
15 |
| -// // method chaining is also supported |
16 |
| -// // the above is equal to: |
17 |
| -// GetRequest("http://www.example.com"). |
18 |
| -// Set("Accept", "application/json"). |
19 |
| -// Query("foo1", "bar1"). |
20 |
| -// Query("foo2", "bar2"). |
21 |
| -// Build() |
| 13 | +// // Build to a HTTP request |
| 14 | +// req.Build() |
22 | 15 | //
|
23 |
| -// // struct body |
24 |
| -// foo = Foo{Bar: "val"} |
25 |
| -// PostRequest("http://www.example.com"). |
26 |
| -// Body(foo) |
| 16 | +// // method chaining is also supported |
| 17 | +// // the above is equal to: |
| 18 | +// GetRequest("http://www.example.com"). |
| 19 | +// Set("Accept", "application/json"). |
| 20 | +// Query("foo1", "bar1"). |
| 21 | +// Query("foo2", "bar2"). |
| 22 | +// Build() |
27 | 23 | //
|
28 |
| -// // String body |
29 |
| -// PostRequest("http://www.example.com"). |
30 |
| -// Body("{\"bar\": \"val\"}") |
| 24 | +// // struct body |
| 25 | +// foo = Foo{Bar: "val"} |
| 26 | +// PostRequest("http://www.example.com"). |
| 27 | +// Body(foo) |
31 | 28 | //
|
32 |
| -// // Stream body |
33 |
| -// PostRequest("http://www.example.com"). |
34 |
| -// Body(strings.NewReader("abcde")) |
| 29 | +// // String body |
| 30 | +// PostRequest("http://www.example.com"). |
| 31 | +// Body("{\"bar\": \"val\"}") |
35 | 32 | //
|
36 |
| -// // Multipart POST request |
37 |
| -// var f *os.File |
38 |
| -// PostRequest("http://www.example.com"). |
39 |
| -// Field("foo", "bar"). |
40 |
| -// File("file1", File{Name: f.Name(), Content: f}). |
41 |
| -// File("file2", File{Name: "1.txt", Content: []byte("abcde"), Type: "text/plain"}) |
| 33 | +// // Stream body |
| 34 | +// PostRequest("http://www.example.com"). |
| 35 | +// Body(strings.NewReader("abcde")) |
| 36 | +// |
| 37 | +// // Multipart POST request |
| 38 | +// var f *os.File |
| 39 | +// PostRequest("http://www.example.com"). |
| 40 | +// Field("foo", "bar"). |
| 41 | +// File("file1", File{Name: f.Name(), Content: f}). |
| 42 | +// File("file2", File{Name: "1.txt", Content: []byte("abcde"), Type: "text/plain"}) |
42 | 43 | package rest
|
43 | 44 |
|
44 | 45 | import (
|
@@ -79,13 +80,20 @@ type Request struct {
|
79 | 80 | queryParams url.Values
|
80 | 81 | formParams url.Values
|
81 | 82 |
|
| 83 | + basicAuthn *BasicAuthInfo |
| 84 | + |
82 | 85 | // files to upload
|
83 | 86 | files map[string][]File
|
84 | 87 |
|
85 | 88 | // custom request body
|
86 | 89 | body interface{}
|
87 | 90 | }
|
88 | 91 |
|
| 92 | +type BasicAuthInfo struct { |
| 93 | + user string |
| 94 | + pass string |
| 95 | +} |
| 96 | + |
89 | 97 | // NewRequest creates a new request with a given rawUrl.
|
90 | 98 | func NewRequest(rawUrl string) *Request {
|
91 | 99 | return &Request{
|
@@ -165,6 +173,14 @@ func (r *Request) Set(key string, value string) *Request {
|
165 | 173 | return r
|
166 | 174 | }
|
167 | 175 |
|
| 176 | +func (r *Request) SetBasicAuth(user string, pass string) *Request { |
| 177 | + r.basicAuthn = &BasicAuthInfo{ |
| 178 | + user: user, |
| 179 | + pass: pass, |
| 180 | + } |
| 181 | + return r |
| 182 | +} |
| 183 | + |
168 | 184 | // Query appends the key, value pair to the request query which will be
|
169 | 185 | // encoded as url query parameters on HTTP request's url.
|
170 | 186 | func (r *Request) Query(key string, value string) *Request {
|
@@ -210,6 +226,10 @@ func (r *Request) Build() (*http.Request, error) {
|
210 | 226 | return req, err
|
211 | 227 | }
|
212 | 228 |
|
| 229 | + if r.basicAuthn != nil { |
| 230 | + req.SetBasicAuth(r.basicAuthn.user, r.basicAuthn.pass) |
| 231 | + } |
| 232 | + |
213 | 233 | for k, vs := range r.header {
|
214 | 234 | for _, v := range vs {
|
215 | 235 | req.Header.Add(k, v)
|
|
0 commit comments