Documentation
¶
Overview ¶
Package body provides a buffering utility allowing HTTP request and response bodies to be buffered so they can be read multiple times. Normally, the standard library http requests and responses do as little buffering as possible. When logging or other such processing is needed, it makes sense to buffer the outbound/inbound bodies exactly once, if possible, in order to reduce copying to a minimum.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Body ¶
type Body struct {
// contains filtered or unexported fields
}
A Body implements the io.Reader, io.Closer amd fmt.Stringer interfaces by reading from a byte slice. The zero value for Body operates like an empty io.Reader. Unlike a bytes.Reader, a Body provides methods to access the byte slice, which should not be modified in place. In addition, Body also implements io.Closer as a no-op.
func Copy ¶ added in v0.9.2
Copy consumes a reader and returns its contents. If the reader is a *Body or a *bytes.Buffer, no copying is needed.
func CopyBody ¶
CopyBody consumes a reader and returns its contents. If the reader is a *Body or a *bytes.Buffer, no copying is needed. Deprecated: use Copy instead.
func NewBody ¶
NewBody returns a new Body reading from a byte slice. It is similar to bytes.NewBuffer.
func NewBodyString ¶
NewBodyString returns a new Body reading from a string. It is similar to bytes.NewBufferString.
func (*Body) Getter ¶
func (r *Body) Getter() func() (io.ReadCloser, error)
Getter returns a function that allows the body to be read multiple times as used by http.Request.GetBody.
func (*Body) Read ¶
Read reads up to len(p) bytes into p the buffer, stopping if the buffer is drained or p is full. The return value n is the number of bytes read. If the buffer has no data to return, err is io.EOF (unless len(p) is zero); otherwise it is nil.