Body

ctx.Request.Body provides access to the HTTP request body.

Unlike Formwhich only processes requests application/x-www-form-urlencoded and multipart/form-data, Body allows access to the original content of the request regardless of the type of content sent by the client.

Depending on the application needs, the body can be read as binary data, as text, decoded from JSON or copied directly to any io.Writer.

During the processing of an request, ctx.Body is an alias of ctx.Request.Body, so both provide exactly the same functionality.

When the request comes from an interface developed with GOX, it is recommended to use Actions to receive information sent from the browser. Body is especially useful for HTTP, REST API and any scenario where it is necessary to access the content of the request directly.


Structure

ctx.Request.Body implement the following API:

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

Methods

MethodDescription
GetBytes()Returns the body of the request as []byte.
ToText()Returns the body of the request as string.
ToJSON()Decode a JSON document within the value provided.
WriteTo()Copy the body of the request to a io.Writer.

GetBytes()

Firma:func (Body) GetBytes() ([]byte, error)

GetBytes() returns the full body of the request as a []byte.

go
func POST(ctx *gox.Context) gox.Page {
    data, err := ctx.Body.GetBytes()

    if err != nil {
        return <p>Request body unavailable.</p>
    }

    return <p>{{ len(data) }}</p>
}

This method can be used with any type of content.

Once loaded, the body remains available for post- GetBytes(), ToText() and ToJSON().


ToText()

Firma:func (Body) ToText() (string, error)

ToText() returns the full body of the request as a string of text.

go
func POST(ctx *gox.Context) gox.Page {
    text, err := ctx.Body.ToText()

    if err != nil {
        return err
    }

    return <pre>{{ text }}</pre>
}

This method does not value the type of content of the request and can be used with any Content-Type.


ToJSON()

Firma:func (Body) ToJSON(out any) error

ToJSON() decodifies the body of the request as a JSON document.

go
type UserInput struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

func POST(ctx *gox.Context) gox.Page {
    var input UserInput

    if err := ctx.Body.ToJSON(&input); err != nil {
        return <p>Invalid JSON.</p>
    }

    return <p>{{ input.Name }}</p>
}

The argument must be a valid destination for the decoding of the JSON document.

This method only accepts requests whose Content-Type corresponds to a JSON type.


WriteTo()

Firma:func (Body) WriteTo(dst io.Writer) error

WriteTo() copy the body of the request directly to any io.Writer.

go
func POST(ctx *gox.Context) gox.Page {
    file, err := os.Create("upload.bin")

    if err != nil {
        return err
    }

    defer file.Close()

    if err := ctx.Body.WriteTo(file); err != nil {
        return err
    }

    return <p>Upload completed.</p>
}

This method is useful for transferring the content of the request without processing it in advance.

Yeah. WriteTo() directly consumes the body before it is read by another method, the content will no longer be available for subsequent calls.


Best practices

  • Use GetBytes() when you need to work with binary data.
  • Use ToText() for textual content.
  • Use ToJSON() for JSON requests.
  • Use WriteTo() when you need to copy the content directly to another io.Writer.
  • Use Form to work with HTML forms.
  • Use Actions when the request comes from an interface developed with GOX.

To learn more about FileSee your documentation. To learn more about Response, check your documentation.


See also

Next step

Continue with Session to learn how to keep information among the requests associated with the same client.

Session