ctx.Response equals to http.ResponseWriter.

Text

  1. func text(ctx *clevergo.Context) error {
  2. return ctx.String(http.StatusOK, "hello world")
  3. }

HTML

  1. func html(ctx *clevergo.Context) error {
  2. return ctx.HTML(http.StatusOK, "<html><body>hello world</body></html>")
  3. }
  4. func htmlBlob(ctx *clevergo.Context) error {
  5. return ctx.HTMLBlob(http.StatusOK, []byte("<html><body>hello world</body></html>"))
  6. }

Render

Renders a template, see Views.

JSON

  1. func json(ctx *clevergo.Context) error {
  2. return ctx.JSON(http.StatusOK, map[string]interface{}{
  3. "message": "hello world",
  4. })
  5. }
  6. func jsonBlob(ctx *clevergo.Context) error {
  7. return ctx.JSONBlob([]byte(`{"message":"hello world"}`))
  8. }

JSONP

  1. func jsonp(ctx *clevergo.Context) error {
  2. return ctx.JSONP(http.StatusOK, map[string]interface{}{
  3. "message": "hello world",
  4. })
  5. }
  6. func jsonpCallback(ctx *clevergo.Context) error {
  7. return ctx.JSONPCallback(http.StatusOK, "myCallback", map[string]interface{}{
  8. "message": "hello world",
  9. })
  10. }

XML

  1. func xml(ctx *clevergo.Context) error {
  2. return ctx.XML(http.StatusOK, map[string]interface{}{
  3. "message": "hello world",
  4. })
  5. }
  6. func xmlBlob(ctx *clevergo.Context) error {
  7. return ctx.XMLBlob(http.StatusOK, []byte(`<xml><message>hello world</message></xml>`))
  8. }

Emit

Emit emits a response with the given code, content type and string data.

  1. func emit(ctx *clevergo.Context) error {
  2. return ctx.Emit(http.StatusOK, "text/html", "data")
  3. }

Blob

Blob emits a response with the given code, content type and byte slice data.

  1. func blob(ctx *clevergo.Context) error {
  2. return ctx.Blob(http.StatusOK, "text/html", []byte("data"))
  3. }

Redirect

  1. func redirect(ctx *clevergo.Context) error {
  2. ctx.Redirect("/login", http.StatusFound)
  3. return nil
  4. }

SendFile

Sends a file to browser.

  1. func download(ctx *clevergo.Context) error {
  2. buf := bytes.NewReader([]byte("bar"))
  3. return ctx.SendFile("foo.txt", buf)
  4. }

Shortcuts

Method
Context.Error http.Error
Context.NotFound http.NotFoud
Context.Redirect http.Redirect
Context.SetCookie http.SetCookie
Context.Write http.ResponseWriter.Write
Context.WriteHeader http.ResponseWriter.WriteHeader