Ir al contenido

Response

Un handler escribe su response a través de echo.Context. Cada helper establece por ti el Content-Type adecuado y el código de estado.

Context#String(code int, s string) envía una response de texto plano con un código de estado.

func(c *echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
}

Context#HTML(code int, html string) envía una response HTML simple con un código de estado. Para generar HTML dinámicamente, consulta Templates.

func(c *echo.Context) error {
return c.HTML(http.StatusOK, "<strong>Hello, World!</strong>")
}

Context#HTMLBlob(code int, b []byte) envía un blob HTML con un código de estado. Es útil con un template engine que produce []byte.

func handler(c *echo.Context) error {
blob := []byte("<strong>Hello, World!</strong>")
return c.HTMLBlob(http.StatusOK, blob)
}

Consulta Templates.

Context#JSON(code int, i any) codifica un valor Go como JSON y lo envía con un código de estado.

type User struct {
Name string `json:"name" xml:"name"`
Email string `json:"email" xml:"email"`
}
func(c *echo.Context) error {
u := &User{
Name: "Jon",
}
return c.JSON(http.StatusOK, u)
}

Context#JSON() usa json.Marshal internamente, lo que puede ser ineficiente para payloads grandes. En ese caso, transmite el JSON directamente:

func(c *echo.Context) error {
u := &User{
Name: "Jon",
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
c.Response().WriteHeader(http.StatusOK)
return json.NewEncoder(c.Response()).Encode(u)
}

Context#JSONPretty(code int, i any, indent string) envía una response JSON con formato bonito. La indentación puede ser espacios o tabs.

func(c *echo.Context) error {
u := &User{
Name: "Jon",
}
return c.JSONPretty(http.StatusOK, u, " ")
}
{
"email": "[email protected]",
"name": "Jon"
}

Context#JSONBlob(code int, b []byte) envía directamente un blob JSON precodificado, por ejemplo desde una base de datos.

func(c *echo.Context) error {
encodedJSON := []byte{} // Encoded JSON from an external source.
return c.JSONBlob(http.StatusOK, encodedJSON)
}

Context#JSONP(code int, callback string, i any) codifica un valor Go como JSON y lo envía como payload JSONP envuelto en el callback dado.

func handler(c *echo.Context) error {
callback := c.QueryParam("callback")
return c.JSONP(http.StatusOK, callback, &User{Name: "Jon", Email: "[email protected]"})
}

Consulta el recetario de JSONP.

Context#XML(code int, i any) codifica un valor Go como XML y lo envía con un código de estado.

func(c *echo.Context) error {
u := &User{
Name: "Jon",
}
return c.XML(http.StatusOK, u)
}

Context#XML usa xml.Marshal internamente, lo que puede ser ineficiente para payloads grandes. En ese caso, transmite el XML directamente:

func(c *echo.Context) error {
u := &User{
Name: "Jon",
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationXMLCharsetUTF8)
c.Response().WriteHeader(http.StatusOK)
return xml.NewEncoder(c.Response()).Encode(u)
}

Context#XMLPretty(code int, i any, indent string) envía una response XML con formato bonito. La indentación puede ser espacios o tabs.

func(c *echo.Context) error {
u := &User{
Name: "Jon",
}
return c.XMLPretty(http.StatusOK, u, " ")
}
<?xml version="1.0" encoding="UTF-8"?>
<User>
<Name>Jon</Name>
<Email>[email protected]</Email>
</User>

Context#XMLBlob(code int, b []byte) envía directamente un blob XML precodificado, por ejemplo desde una base de datos.

func(c *echo.Context) error {
encodedXML := []byte{} // Encoded XML from an external source.
return c.XMLBlob(http.StatusOK, encodedXML)
}

Context#File(file string) envía el contenido de un archivo como response. Establece el content type correcto y maneja el caching automáticamente.

func(c *echo.Context) error {
return c.File("<PATH_TO_YOUR_FILE>")
}

Context#Attachment(file, name string) es como File(), pero envía el archivo con Content-Disposition: attachment y el nombre dado.

func(c *echo.Context) error {
return c.Attachment("<PATH_TO_YOUR_FILE>", "<ATTACHMENT_NAME>")
}

Context#Inline(file, name string) es como File(), pero envía el archivo con Content-Disposition: inline y el nombre dado.

func(c *echo.Context) error {
return c.Inline("<PATH_TO_YOUR_FILE>", "<INLINE_NAME>")
}

Context#Blob(code int, contentType string, b []byte) envía datos arbitrarios con un content type y código de estado dados.

func(c *echo.Context) error {
data := []byte(`0306703,0035866,NO_ACTION,06/19/2006
0086003,"0005866",UPDATED,06/19/2006`)
return c.Blob(http.StatusOK, "text/csv", data)
}

Context#Stream(code int, contentType string, r io.Reader) envía un stream de datos arbitrario con un content type, io.Reader y código de estado dados.

func(c *echo.Context) error {
f, err := os.Open("<PATH_TO_IMAGE>")
if err != nil {
return err
}
defer f.Close()
return c.Stream(http.StatusOK, "image/png", f)
}

Context#NoContent(code int) envía un body vacío con un código de estado.

func(c *echo.Context) error {
return c.NoContent(http.StatusOK)
}

Context#Redirect(code int, url string) redirige el request a la URL dada con un código de estado.

func(c *echo.Context) error {
return c.Redirect(http.StatusMovedPermanently, "<URL>")
}

Response#Before(func()) registra una función que se ejecuta justo antes de escribir la response.

Response#After(func()) registra una función que se ejecuta justo después de escribir la response. Si Content-Length es desconocido, no se ejecuta ninguna función after.

e.GET("/hooks", func(c *echo.Context) error {
resp, err := echo.UnwrapResponse(c.Response())
if err != nil {
return err
}
resp.Before(func() {
println("before response")
})
resp.After(func() {
println("after response")
})
return c.String(http.StatusOK, "Hello, World!")
})