Response

Send String

Context#String(code int, s string) can be used to send plain text response with status code.

Example

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

Send HTML (Reference to templates)

Context#HTML(code int, html string) can be used to send simple HTML response with status code. If you are looking to send dynamically generate HTML see templates.

Example

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

Send HTML Blob

Context#HTMLBlob(code int, b []byte) can be used to send HTML blob with status code. You may find it handy using with a template engine which outputs []byte.

Render Template

Learn more

Send JSON

Context#JSON(code int, i interface{}) can be used to encode a provided Go type into JSON and send it as response with status code.

Example

  1. // User
  2. type User struct {
  3. Name string `json:"name" xml:"name"`
  4. Email string `json:"email" xml:"email"`
  5. }
  6. // Handler
  7. func(c echo.Context) error {
  8. u := &User{
  9. Name: "Jon",
  10. Email: "[email protected]",
  11. }
  12. return c.JSON(http.StatusOK, u)
  13. }

Stream JSON

Context#JSON() internally uses json.Marshal which may not be efficient to large JSON, in that case you can directly stream JSON.

Example

  1. func(c echo.Context) error {
  2. u := &User{
  3. Name: "Jon",
  4. Email: "[email protected]",
  5. }
  6. c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
  7. c.Response().WriteHeader(http.StatusOK)
  8. return json.NewEncoder(c.Response()).Encode(u)
  9. }

JSON Pretty

Context#JSONPretty(code int, i interface{}, indent string) can be used to a send a JSON response which is pretty printed based on indent, which could be spaces or tabs.

Example below sends a pretty print JSON indented with spaces:

  1. func(c echo.Context) error {
  2. u := &User{
  3. Name: "Jon",
  4. Email: "[email protected]",
  5. }
  6. return c.JSONPretty(http.StatusOK, u, " ")
  7. }
  1. {
  2. "email": "[email protected]",
  3. "name": "Jon"
  4. }

Response - 图1tip

You can also use Context#JSON() to output a pretty printed JSON (indented with spaces) by appending pretty in the request URL query string.

Example

  1. curl http://localhost:1323/users/1?pretty

JSON Blob

Context#JSONBlob(code int, b []byte) can be used to send pre-encoded JSON blob directly from external source, for example, database.

Example

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

Send JSONP

Context#JSONP(code int, callback string, i interface{}) can be used to encode a provided Go type into JSON and send it as JSONP payload constructed using a callback, with status code.

Example

Send XML

Context#XML(code int, i interface{}) can be used to encode a provided Go type into XML and send it as response with status code.

Example

  1. func(c echo.Context) error {
  2. u := &User{
  3. Name: "Jon",
  4. Email: "[email protected]",
  5. }
  6. return c.XML(http.StatusOK, u)
  7. }

Stream XML

Context#XML internally uses xml.Marshal which may not be efficient to large XML, in that case you can directly stream XML.

Example

  1. func(c echo.Context) error {
  2. u := &User{
  3. Name: "Jon",
  4. Email: "[email protected]",
  5. }
  6. c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationXMLCharsetUTF8)
  7. c.Response().WriteHeader(http.StatusOK)
  8. return xml.NewEncoder(c.Response()).Encode(u)
  9. }

XML Pretty

Context#XMLPretty(code int, i interface{}, indent string) can be used to a send an XML response which is pretty printed based on indent, which could be spaces or tabs.

Example below sends a pretty print XML indented with spaces:

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

Response - 图2tip

You can also use Context#XML() to output a pretty printed XML (indented with spaces) by appending pretty in the request URL query string.

Example

  1. curl http://localhost:1323/users/1?pretty

XML Blob

Context#XMLBlob(code int, b []byte) can be used to send pre-encoded XML blob directly from external source, for example, database.

Example

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

Send File

Context#File(file string) can be used to send the content of file as response. It automatically sets the correct content type and handles caching gracefully.

Example

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

Send Attachment

Context#Attachment(file, name string) is similar to File() except that it is used to send file as attachment with provided name.

Example

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

Send Inline

Context#Inline(file, name string) is similar to File() except that it is used to send file as inline with provided name.

Example

  1. func(c echo.Context) error {
  2. return c.Inline("<PATH_TO_YOUR_FILE>")
  3. }

Send Blob

Context#Blob(code int, contentType string, b []byte) can be used to send an arbitrary data response with provided content type and status code.

Example

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

Send Stream

Context#Stream(code int, contentType string, r io.Reader) can be used to send an arbitrary data stream response with provided content type, io.Reader and status code.

Example

  1. func(c echo.Context) error {
  2. f, err := os.Open("<PATH_TO_IMAGE>")
  3. if err != nil {
  4. return err
  5. }
  6. return c.Stream(http.StatusOK, "image/png", f)
  7. }

Send No Content

Context#NoContent(code int) can be used to send empty body with status code.

Example

  1. func(c echo.Context) error {
  2. return c.NoContent(http.StatusOK)
  3. }

Redirect Request

Context#Redirect(code int, url string) can be used to redirect the request to a provided URL with status code.

Example

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

Hooks

Before Response

Context#Response#Before(func()) can be used to register a function which is called just before the response is written.

After Response

Context#Response#After(func()) can be used to register a function which is called just after the response is written. If the “Content-Length” is unknown, none of the after function is executed.

Example

  1. func(c echo.Context) error {
  2. c.Response().Before(func() {
  3. println("before response")
  4. })
  5. c.Response().After(func() {
  6. println("after response")
  7. })
  8. return c.NoContent(http.StatusNoContent)
  9. }

Response - 图3tip

It is possible to register multiple Before and After functions.