Response
A handler writes its response through the echo.Context. Each helper sets the
appropriate Content-Type and status code for you.
Send string
Section titled “Send string”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!")}Send HTML
Section titled “Send HTML”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>")}Send HTML blob
Section titled “Send HTML blob”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)}Render template
Section titled “Render template”See Templates.
Send JSON
Section titled “Send JSON”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)}Stream JSON
Section titled “Stream JSON”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)}JSON pretty
Section titled “JSON pretty”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, " ")}{ "name": "Jon"}JSON blob
Section titled “JSON blob”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)}Send JSONP
Section titled “Send JSONP”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")}See the JSONP cookbook.
Send XML
Section titled “Send XML”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)}Stream XML
Section titled “Stream XML”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)}XML pretty
Section titled “XML pretty”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></User>XML blob
Section titled “XML blob”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)}Send file
Section titled “Send file”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>")}Send attachment
Section titled “Send attachment”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>")}Send inline
Section titled “Send inline”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>")}Send blob
Section titled “Send blob”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/20060086003,"0005866",UPDATED,06/19/2006`) return c.Blob(http.StatusOK, "text/csv", data)}Send stream
Section titled “Send stream”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)}Send no content
Section titled “Send no content”Context#NoContent(code int) sends an empty body with a status code.
func(c *echo.Context) error { return c.NoContent(http.StatusOK)}Redirect request
Section titled “Redirect request”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>")}Before response
Section titled “Before response”Response#Before(func()) registers a function that runs just before the response is
written.
After response
Section titled “After response”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!")})