Body

Body defines the reading and decoding operations of the body of an request.

This API is used in Body of request.


Symbols

SymbolClassDescription
BodytypeBody defines a public type of API.
NewBodyfuncBuild NewBody.
body.GetBytesfuncReturns the whole body as bytes.
body.ToTextfuncReturns the whole body as text.
body.ToJSONfuncDecodes the JSON body into a destination.
body.WriteTofuncCopy the content to a io.Writer.
NewBodyFromBytesfuncBuild NewBodyFromBytes.

Body

Declaration:

gox
type Body interface {
	GetBytes() ([]byte, error)
	ToText() (string, error)
	ToJSON(out any) error
	WriteTo(dst io.Writer) error
}

Body defines a public type of API.


NewBody

Declaration:

gox
func NewBody(reader io.ReadCloser, contentType string) Body

Build NewBody.

Parameters

ParameterTypeDescription
readerio.ReadCloserReading source.
contentTypestringMIME type of content.

Returns

ReturnTypeDescription
valueBodyType value Body produced by the operation.

Usage syntax

gox
value := gox.NewBody(reader, "application/octet-stream")

body.GetBytes

Declaration:

gox
func (b *body) GetBytes() ([]byte, error)

Returns the whole body as bytes.

Returns

ReturnTypeDescription
value[]byteResult collection; can be nil when there are no values.
errerrornil if the operation ends correctly; otherwise, the error produced.

Behavior

The operation returns an error when it cannot complete the above contract.

Errors

GetBytes returns an error when the arguments, the status of the receiver or the underlying operation make it impossible to complete the call.

Usage syntax

gox
value, err := body.GetBytes()

body.ToText

Declaration:

gox
func (b *body) ToText() (string, error)

Returns the whole body as text.

Returns

ReturnTypeDescription
valuestringTextual value produced by the operation; its zero value is an empty string.
errerrornil if the operation ends correctly; otherwise, the error produced.

Behavior

The operation returns an error when it cannot complete the above contract.

Errors

ToText returns an error when the arguments, the status of the receiver or the underlying operation make it impossible to complete the call.

Usage syntax

gox
value, err := body.ToText()

body.ToJSON

Declaration:

gox
func (b *body) ToJSON(out any) error

Decodes the JSON body into a destination.

Parameters

ParameterTypeDescription
outanyDestination where the result is decoded.

Returns

ReturnTypeDescription
errerrornil if the operation ends correctly; otherwise, the error produced.

Behavior

The argument must be a valid destination for encoding/json.

Errors

ToJSON returns an error when the arguments, the status of the receiver or the underlying operation make it impossible to complete the call.

Usage syntax

gox
err := body.ToJSON(&value)

body.WriteTo

Declaration:

gox
func (b *body) WriteTo(dst io.Writer) error

Copy the content to a io.Writer.

Parameters

ParameterTypeDescription
dstio.WriterA writing destination.

Returns

ReturnTypeDescription
errerrornil if the operation ends correctly; otherwise, the error produced.

Behavior

The operation returns an error when it cannot complete the above contract.

Errors

WriteTo returns an error when the arguments, the status of the receiver or the underlying operation make it impossible to complete the call.

Usage syntax

gox
err := body.WriteTo(value)

NewBodyFromBytes

Declaration:

gox
func NewBodyFromBytes(data []byte, contentType string) Body

Build NewBodyFromBytes.

Parameters

ParameterTypeDescription
data[]byteType value []byte required by the operation.
contentTypestringMIME type of content.

Returns

ReturnTypeDescription
valueBodyType value Body produced by the operation.

Usage syntax

gox
value := gox.NewBodyFromBytes(data, "application/octet-stream")

See also

Next step

Continue with status and operations of Response.

Response