Response
Send String
Context#String(code int, s string)
can be used to send plain text response with statuscode.
Example
func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
}
Send HTML (Reference to templates)
Context#HTML(code int, html string)
can be used to send simple HTML response withstatus code. If you are looking to send dynamically generate HTML see templates.
Example
func(c echo.Context) error {
return c.HTML(http.StatusOK, "<strong>Hello, World!</strong>")
}
Send HTML Blob
Context#HTMLBlob(code int, b []byte)
can be used to send HTML blob with statuscode. You may find it handy using with a template engine which outputs []byte
.
Render Template
Send JSON
Context#JSON(code int, i interface{})
can be used to encode a provided Go type intoJSON and send it as response with status code.
Example
// User
type User struct {
Name string `json:"name" xml:"name"`
Email string `json:"email" xml:"email"`
}
// Handler
func(c echo.Context) error {
u := &User{
Name: "Jon",
Email: "[email protected]",
}
return c.JSON(http.StatusOK, u)
}
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
func(c echo.Context) error {
u := &User{
Name: "Jon",
Email: "[email protected]",
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
c.Response().WriteHeader(http.StatusOK)
return json.NewEncoder(c.Response()).Encode(u)
}
JSON Pretty
Context#JSONPretty(code int, i interface{}, indent string)
can be used to a senda 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:
func(c echo.Context) error {
u := &User{
Name: "Jon",
Email: "[email protected]",
}
return c.JSONPretty(http.StatusOK, u, " ")
}
{
"email": "[email protected]",
"name": "Jon"
}
You can also use
Context#JSON()
to output a pretty printed JSON (indented with spaces)by appendingpretty
in the request URL query string.
Example
curl http://localhost:1323/users/1?pretty
JSON Blob
Context#JSONBlob(code int, b []byte)
can be used to send pre-encoded JSON blob directlyfrom external source, for example, database.
Example
func(c echo.Context) error {
encodedJSON := []byte{} // Encoded JSON from external source
return c.JSONBlob(http.StatusOK, encodedJSON)
}
Send JSONP
Context#JSONP(code int, callback string, i interface{})
can be used to encode a providedGo type into JSON and send it as JSONP payload constructed using a callback, withstatus code.
Send XML
Context#XML(code int, i interface{})
can be used to encode a provided Go type intoXML and send it as response with status code.
Example
func(c echo.Context) error {
u := &User{
Name: "Jon",
Email: "[email protected]",
}
return c.XML(http.StatusOK, u)
}
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
func(c echo.Context) error {
u := &User{
Name: "Jon",
Email: "[email protected]",
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationXMLCharsetUTF8)
c.Response().WriteHeader(http.StatusOK)
return xml.NewEncoder(c.Response()).Encode(u)
}
XML Pretty
Context#XMLPretty(code int, i interface{}, indent string)
can be used to a sendan 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:
func(c echo.Context) error {
u := &User{
Name: "Jon",
Email: "[email protected]",
}
return c.XMLPretty(http.StatusOK, u, " ")
}
<?xml version="1.0" encoding="UTF-8"?>
<User>
<Name>Jon</Name>
<Email>[email protected]</Email>
</User>
You can also use
Context#XML()
to output a pretty printed XML (indented with spaces) by appendingpretty
in the request URL query string.
Example
curl http://localhost:1323/users/1?pretty
XML Blob
Context#XMLBlob(code int, b []byte)
can be used to send pre-encoded XML blob directlyfrom external source, for example, database.
Example
func(c echo.Context) error {
encodedXML := []byte{} // Encoded XML from external source
return c.XMLBlob(http.StatusOK, encodedXML)
}
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
func(c echo.Context) error {
return c.File("<PATH_TO_YOUR_FILE>")
}
Send Attachment
Context#Attachment(file, name string)
is similar to File()
except that it isused to send file as attachment with provided name.
Example
func(c echo.Context) error {
return c.Attachment("<PATH_TO_YOUR_FILE>")
}
Send Inline
Context#Inline(file, name string)
is similar to File()
except that it isused to send file as inline with provided name.
Example
func(c echo.Context) error {
return c.Inline("<PATH_TO_YOUR_FILE>")
}
Send Blob
Context#Blob(code int, contentType string, b []byte)
can be used to send an arbitrarydata response with provided content type and status code.
Example
func(c echo.Context) (err error) {
data := []byte(`0306703,0035866,NO_ACTION,06/19/2006
0086003,"0005866",UPDATED,06/19/2006`)
return c.Blob(http.StatusOK, "text/csv", data)
}
Send Stream
Context#Stream(code int, contentType string, r io.Reader)
can be used to send anarbitrary data stream response with provided content type, io.Reader
and statuscode.
Example
func(c echo.Context) error {
f, err := os.Open("<PATH_TO_IMAGE>")
if err != nil {
return err
}
return c.Stream(http.StatusOK, "image/png", f)
}
Send No Content
Context#NoContent(code int)
can be used to send empty body with status code.
Example
func(c echo.Context) error {
return c.NoContent(http.StatusOK)
}
Redirect Request
Context#Redirect(code int, url string)
can be used to redirect the request toa provided URL with status code.
Example
func(c echo.Context) error {
return c.Redirect(http.StatusMovedPermanently, "<URL>")
}
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 justafter the response is written. If the “Content-Length” is unknown, none of the afterfunction is executed.
Example
func(c echo.Context) error {
c.Response().Before(func() {
println("before response")
})
c.Response().After(func() {
println("after response")
})
return c.NoContent(http.StatusNoContent)
}
It is possible to register multiple before and after functions