-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
33 lines (30 loc) · 877 Bytes
/
util.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
package requests
import (
"bytes"
"io"
"net/http"
)
// ParseBody parse body from `Request.Body`.
func ParseBody(r io.ReadCloser) (*bytes.Buffer, error) {
var buf bytes.Buffer
if r == nil || r == http.NoBody {
// No copying needed. Preserve the magic sentinel meaning of NoBody.
return &buf, nil
}
if _, err := buf.ReadFrom(r); err != nil {
return &buf, err
}
return &buf, r.Close()
}
// CopyBody reads all of b to memory and then returns two equivalent
// ReadClosers yielding the same bytes.
//
// It returns an error if the initial slurp of all bytes fails. It does not attempt
// to make the returned ReadClosers have identical error-matching behavior.
func CopyBody(b io.ReadCloser) (*bytes.Buffer, io.ReadCloser, error) {
buf, err := ParseBody(b)
if err != nil {
return nil, nil, err
}
return buf, io.NopCloser(bytes.NewReader(buf.Bytes())), nil
}