body

package
v0.48.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 3 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

View Source
var JsonMarshal = json.Marshal

JsonMarshal marshals a value as JSON. To replace this with a Jsoniter implementation, set

rest.JsonMarshalToString = jsoniter.JSON.Marshal
View Source
var JsonMarshalToString = func(v any) (string, error) {
	bs, err := JsonMarshal(v)
	return string(bs), err
}

JsonMarshalToString marshals a value as a JSON string. To replace this with a Jsoniter implementation, set

rest.JsonMarshalToString = jsoniter.JSON.MarshalToString
View Source
var JsonUnmarshal = func(r io.Reader, output any) error {
	decoder := json.NewDecoder(r)
	decoder.UseNumber()
	return decoder.Decode(output)
}

JsonUnmarshal unmarshals a value. The decoder UseNumber() option is set; replace this function if necessary. To replace this with a Jsoniter implementation, set

rest.JsonUnmarshal = func(r io.Reader, output any) error {
	decoder := jsoniter.JSON.NewDecoder(r)
	decoder.UseNumber()
	return decoder.Decode(output)
}

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

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

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

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. Deprecated: use Copy instead.

func JSON added in v0.37.0

func JSON(value any) *Body

JSON encodes some value as a Body containing JSON data. This is a convenience for creating request entities. Any JSON encoding errors are silently discarded (e.g. from attempting to encode a Go channel).

func MustCopy added in v0.9.2

func MustCopy(rdr io.Reader) *Body

MustCopy is the same as Copy except that it panics on error.

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 (b *Body) Buffer() *bytes.Buffer

Buffer gets the data in a form that is well suited to http.Request.Body. b may be nil, in which case nil is returned.

func (*Body) Bytes

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

Bytes gets the byte slice regardless of the current read position. b may be nil, in which case a nil slice is returned.

func (*Body) Close

func (r *Body) Close() error

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

func (*Body) Getter

func (b *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. b may be nil.

func (*Body) Read

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

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.

func (*Body) Rewind

func (b *Body) Rewind() *Body

Rewind rewinds the read pointer in the Body to zero. It returns the modified Body. See Body.Read. b may be nil.

func (*Body) String

func (b *Body) String() string

String gets the byte slice as a string regardless of the current read position. b may be nil, which yields a blank string.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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