Skip to content

Response

A handler writes its response through the echo.Context. Each helper sets the appropriate Content-Type and status code for you.

Context#String(code int, s string) sends a plain text response with a status code.

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

Context#HTML(code int, html string) sends a simple HTML response with a status code. To generate HTML dynamically, see Templates.

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

Context#HTMLBlob(code int, b []byte) sends an HTML blob with a status code. It is handy with a template engine that outputs []byte.

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

See Templates.

Context#JSON(code int, i any) encodes a Go value as JSON and sends it with a status code.

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() uses json.Marshal internally, which may be inefficient for large payloads. In that case, stream the JSON directly:

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) sends a pretty-printed JSON response. The indent can be spaces or 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) sends a pre-encoded JSON blob directly, for example from a database.

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) encodes a Go value as JSON and sends it as a JSONP payload wrapped in the given callback.

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

See the JSONP cookbook.

Context#XML(code int, i any) encodes a Go value as XML and sends it with a status code.

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

Context#XML uses xml.Marshal internally, which may be inefficient for large payloads. In that case, stream the XML directly:

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) sends a pretty-printed XML response. The indent can be spaces or 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) sends a pre-encoded XML blob directly, for example from a database.

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

Context#File(file string) sends the contents of a file as the response. It sets the correct content type and handles caching automatically.

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

Context#Attachment(file, name string) is like File() but sends the file with Content-Disposition: attachment and the given name.

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

Context#Inline(file, name string) is like File() but sends the file with Content-Disposition: inline and the given name.

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

Context#Blob(code int, contentType string, b []byte) sends arbitrary data with a given content type and status code.

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) sends an arbitrary data stream with a given content type, io.Reader, and status code.

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) sends an empty body with a status code.

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

Context#Redirect(code int, url string) redirects the request to the given URL with a status code.

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

Response#Before(func()) registers a function that runs just before the response is written.

Response#After(func()) registers a function that runs just after the response is written. If the Content-Length is unknown, no after functions run.

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!")
})