Body
Body defines the reading and decoding operations of the body of an request.
This API is used in Body of request.
Symbols
| Symbol | Class | Description |
|---|---|---|
| Body | type | Body defines a public type of API. |
| NewBody | func | Build NewBody. |
| body.GetBytes | func | Returns the whole body as bytes. |
| body.ToText | func | Returns the whole body as text. |
| body.ToJSON | func | Decodes the JSON body into a destination. |
| body.WriteTo | func | Copy the content to a io.Writer. |
| NewBodyFromBytes | func | Build NewBodyFromBytes. |
Body
Declaration:
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:
func NewBody(reader io.ReadCloser, contentType string) Body Build NewBody.
Parameters
| Parameter | Type | Description |
|---|---|---|
reader | io.ReadCloser | Reading source. |
contentType | string | MIME type of content. |
Returns
| Return | Type | Description |
|---|---|---|
value | Body | Type value Body produced by the operation. |
Usage syntax
value := gox.NewBody(reader, "application/octet-stream") body.GetBytes
Declaration:
func (b *body) GetBytes() ([]byte, error) Returns the whole body as bytes.
Returns
| Return | Type | Description |
|---|---|---|
value | []byte | Result collection; can be nil when there are no values. |
err | error | nil 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
value, err := body.GetBytes() body.ToText
Declaration:
func (b *body) ToText() (string, error) Returns the whole body as text.
Returns
| Return | Type | Description |
|---|---|---|
value | string | Textual value produced by the operation; its zero value is an empty string. |
err | error | nil 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
value, err := body.ToText() body.ToJSON
Declaration:
func (b *body) ToJSON(out any) error Decodes the JSON body into a destination.
Parameters
| Parameter | Type | Description |
|---|---|---|
out | any | Destination where the result is decoded. |
Returns
| Return | Type | Description |
|---|---|---|
err | error | nil 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
err := body.ToJSON(&value) body.WriteTo
Declaration:
func (b *body) WriteTo(dst io.Writer) error Copy the content to a io.Writer.
Parameters
| Parameter | Type | Description |
|---|---|---|
dst | io.Writer | A writing destination. |
Returns
| Return | Type | Description |
|---|---|---|
err | error | nil 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
err := body.WriteTo(value) NewBodyFromBytes
Declaration:
func NewBodyFromBytes(data []byte, contentType string) Body Build NewBodyFromBytes.
Parameters
| Parameter | Type | Description |
|---|---|---|
data | []byte | Type value []byte required by the operation. |
contentType | string | MIME type of content. |
Returns
| Return | Type | Description |
|---|---|---|
value | Body | Type value Body produced by the operation. |
Usage syntax
value := gox.NewBodyFromBytes(data, "application/octet-stream")