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 ¶
var JsonMarshal = json.Marshal
JsonMarshal marshals a value as JSON. To replace this with a Jsoniter implementation, set
rest.JsonMarshalToString = jsoniter.JSON.Marshal
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
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
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 JSON ¶ added in v0.37.0
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 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) 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 ¶
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) 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 ¶
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.