-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
43 lines (41 loc) · 1 KB
/
request.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
package requestclient
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
// NewRequest returns a new Request given a method, URL, and optional body.
//
// If the provided body is also an io.Closer, the returned
// Request.Body is set to body and will be closed by the Client
// methods Do, Post, and PostForm, and Transport.RoundTrip.
func (r *RequestClient) NewRequest(method string, u *url.URL, body io.Reader) (req *http.Request) {
rc, ok := body.(io.ReadCloser)
if !ok && body != nil {
rc = ioutil.NopCloser(body)
}
req = &http.Request{
Method: method,
URL: u,
Proto: r.RequestProto,
ProtoMajor: r.RequestProtoMajor,
ProtoMinor: r.RequestProtoMinor,
Header: r.Headers,
Body: rc,
Host: u.Host,
}
if body != nil {
switch v := body.(type) {
case *bytes.Buffer:
req.ContentLength = int64(v.Len())
case *bytes.Reader:
req.ContentLength = int64(v.Len())
case *strings.Reader:
req.ContentLength = int64(v.Len())
}
}
return req
}