コンテンツにスキップ

レスポンス

ハンドラは echo.Context を通じてレスポンスを書き込みます。各ヘルパーは適切な Content-Type とステータスコードを設定します。

Context#String(code int, s string) は、ステータスコード付きのプレーンテキストレスポンスを送信します。

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

Context#HTML(code int, html string) は、ステータスコード付きのシンプルな HTML レスポンスを送信します。 HTML を動的に生成するには、テンプレートを参照してください。

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

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 “テンプレートをレンダリングする”

テンプレートを参照してください。

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)
}

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)
}

Context#JSONPretty(code int, i any, indent string) は整形済み JSON レスポンスを送信します。 インデントにはスペースまたはタブを使えます。

func(c *echo.Context) error {
u := &User{
Name: "Jon",
}
return c.JSONPretty(http.StatusOK, u, " ")
}
{
"email": "[email protected]",
"name": "Jon"
}

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)
}

Context#JSONP(code int, callback string, i any) は Go の値を JSON としてエンコードし、 指定した callback でラップした JSONP ペイロードとして送信します。

func handler(c *echo.Context) error {
callback := c.QueryParam("callback")
return c.JSONP(http.StatusOK, callback, &User{Name: "Jon", Email: "[email protected]"})
}

JSONP cookbookを参照してください。

Context#XML(code int, i any) は Go の値を XML としてエンコードし、ステータスコード付きで送信します。

func(c *echo.Context) error {
u := &User{
Name: "Jon",
}
return c.XML(http.StatusOK, u)
}

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)
}

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>
<Email>[email protected]</Email>
</User>

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>")
}

Context#Blob(code int, contentType string, b []byte) は、指定したコンテンツタイプと ステータスコードで任意のデータを送信します。

func(c *echo.Context) error {
data := []byte(`0306703,0035866,NO_ACTION,06/19/2006
0086003,"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)
}

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>")
}

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!")
})