body

package
v0.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2021 License: MIT Imports: 2 Imported by: 0

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 CopyBody

func CopyBody(rdr io.Reader) (*Body, error)

CopyBody consumes a reader and returns its contents. If the reader is a *Body or a *bytes.Buffer, no copying is needed.

func NewBody

func NewBody(b []byte) *Body

NewBody returns a new Body reading from a byte slice. It is similar to bytes.NewBuffer.

func NewBodyString

func NewBodyString(s string) *Body

NewBodyString returns a new Body reading from a string. It is similar to bytes.NewBufferString.

func (*Body) Buffer

func (r *Body) Buffer() *bytes.Buffer

Buffer gets the data in a form that is well suited to http.Request.Body.

func (*Body) Bytes

func (r *Body) Bytes() []byte

Bytes gets the byte slice regardless of the current read position.

func (*Body) Close

func (r *Body) Close() error

Close implements the io.Closer interface as a no-op.

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

func (r *Body) Read(b []byte) (n int, err error)

Read reads the next len(p) bytes from the buffer or until the buffer is drained. 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.

func (*Body) Rewind

func (r *Body) Rewind() *Body

Rewind rewinds the read pointer in the Body to zero and returns the modified Body.

func (*Body) String

func (r *Body) String() string

String gets the byte slice as a string regardless of the current read position.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL