响应
处理函数通过 echo.Context 写入响应。每个辅助方法都会为你设置合适的
Content-Type 和状态码。
Context#String(code int, s string) 会发送带状态码的纯文本响应。
func(c *echo.Context) error { return c.String(http.StatusOK, "Hello, World!")}发送 HTML
Section titled “发送 HTML”Context#HTML(code int, html string) 会发送带状态码的简单 HTML 响应。
要动态生成 HTML,请参见模板。
func(c *echo.Context) error { return c.HTML(http.StatusOK, "<strong>Hello, World!</strong>")}发送 HTML blob
Section titled “发送 HTML blob”Context#HTMLBlob(code int, b []byte) 会发送带状态码的 HTML blob。
它适合与输出 []byte 的模板引擎一起使用。
func handler(c *echo.Context) error { blob := []byte("<strong>Hello, World!</strong>") return c.HTMLBlob(http.StatusOK, blob)}参见模板。
发送 JSON
Section titled “发送 JSON”Context#JSON(code int, i any) 会把 Go 值编码为 JSON,并带状态码发送。
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)}流式发送 JSON
Section titled “流式发送 JSON”Context#JSON() 内部使用 json.Marshal,对于大型负载可能效率不高。
这种情况下,请直接流式发送 JSON:
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) 会发送格式化后的 JSON 响应。
缩进可以是空格或制表符。
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) 会直接发送预编码的 JSON blob,
例如来自数据库的数据。
func(c *echo.Context) error { encodedJSON := []byte{} // Encoded JSON from an external source. return c.JSONBlob(http.StatusOK, encodedJSON)}发送 JSONP
Section titled “发送 JSONP”Context#JSONP(code int, callback string, i any) 会把 Go 值编码为 JSON,
并作为包装在给定 callback 中的 JSONP 负载发送。
func handler(c *echo.Context) error { callback := c.QueryParam("callback")}参见 JSONP cookbook。
发送 XML
Section titled “发送 XML”Context#XML(code int, i any) 会把 Go 值编码为 XML,并带状态码发送。
func(c *echo.Context) error { u := &User{ Name: "Jon", } return c.XML(http.StatusOK, u)}流式发送 XML
Section titled “流式发送 XML”Context#XML 内部使用 xml.Marshal,对于大型负载可能效率不高。
这种情况下,请直接流式发送 XML:
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) 会发送格式化后的 XML 响应。
缩进可以是空格或制表符。
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) 会直接发送预编码的 XML blob,
例如来自数据库的数据。
func(c *echo.Context) error { encodedXML := []byte{} // Encoded XML from an external source. return c.XMLBlob(http.StatusOK, encodedXML)}Context#File(file string) 会把文件内容作为响应发送。它会设置正确的内容类型,
并自动处理缓存。
func(c *echo.Context) error { return c.File("<PATH_TO_YOUR_FILE>")}Context#Attachment(file, name string) 类似于 File(),但会使用
Content-Disposition: attachment 和给定名称发送文件。
func(c *echo.Context) error { return c.Attachment("<PATH_TO_YOUR_FILE>", "<ATTACHMENT_NAME>")}Context#Inline(file, name string) 类似于 File(),但会使用
Content-Disposition: inline 和给定名称发送文件。
func(c *echo.Context) error { return c.Inline("<PATH_TO_YOUR_FILE>", "<INLINE_NAME>")}发送 blob
Section titled “发送 blob”Context#Blob(code int, contentType string, b []byte) 会使用给定内容类型和状态码发送任意数据。
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)}Context#Stream(code int, contentType string, r io.Reader) 会使用给定内容类型、
io.Reader 和状态码发送任意数据流。
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)}发送无内容响应
Section titled “发送无内容响应”Context#NoContent(code int) 会发送带状态码的空响应体。
func(c *echo.Context) error { return c.NoContent(http.StatusOK)}Context#Redirect(code int, url string) 会使用给定 URL 和状态码重定向请求。
func(c *echo.Context) error { return c.Redirect(http.StatusMovedPermanently, "<URL>")}Response#Before(func()) 注册一个函数,在响应写入之前运行。
Response#After(func()) 注册一个函数,在响应写入之后运行。如果 Content-Length
未知,则不会运行任何 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!")})