レスポンス
ハンドラは echo.Context を通じてレスポンスを書き込みます。各ヘルパーは適切な
Content-Type とステータスコードを設定します。
文字列を送信する
Section titled “文字列を送信する”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)}テンプレートをレンダリングする
Section titled “テンプレートをレンダリングする”テンプレートを参照してください。
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)}ファイルを送信する
Section titled “ファイルを送信する”Context#File(file string) はファイルの内容をレスポンスとして送信します。
正しいコンテンツタイプを設定し、キャッシュも自動的に処理します。
func(c *echo.Context) error { return c.File("<PATH_TO_YOUR_FILE>")}添付ファイルを送信する
Section titled “添付ファイルを送信する”Context#Attachment(file, name string) は File() と似ていますが、
Content-Disposition: attachment と指定した名前でファイルを送信します。
func(c *echo.Context) error { return c.Attachment("<PATH_TO_YOUR_FILE>", "<ATTACHMENT_NAME>")}インライン送信する
Section titled “インライン送信する”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)}ストリームを送信する
Section titled “ストリームを送信する”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)}リクエストをリダイレクトする
Section titled “リクエストをリダイレクトする”Context#Redirect(code int, url string) は、指定した URL とステータスコードでリクエストをリダイレクトします。
func(c *echo.Context) error { return c.Redirect(http.StatusMovedPermanently, "<URL>")}レスポンス前
Section titled “レスポンス前”Response#Before(func()) は、レスポンスが書き込まれる直前に実行される関数を登録します。
レスポンス後
Section titled “レスポンス後”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!")})