diff --git a/context.go b/context.go index 6dbcc46b..1b531191 100644 --- a/context.go +++ b/context.go @@ -6,8 +6,6 @@ import ( "net/http" "path/filepath" - "fmt" - "net/url" "bytes" @@ -60,7 +58,7 @@ func (c *Context) Socket() *websocket.Conn { return c.socket } -// Path returns the registered path for a handler. +// Path returns the registered path for the handler. func (c *Context) Path() string { return c.path } @@ -134,31 +132,19 @@ func (c *Context) Render(code int, name string, data interface{}) (err error) { return } -// HTML formats according to a format specifier and sends HTML response with -// status code. -func (c *Context) HTML(code int, format string, a ...interface{}) (err error) { - buf := new(bytes.Buffer) - _, err = fmt.Fprintf(buf, format, a...) - if err != nil { - return err - } +// HTML sends an HTTP response with status code. +func (c *Context) HTML(code int, html string) (err error) { c.response.Header().Set(ContentType, TextHTMLCharsetUTF8) c.response.WriteHeader(code) - c.response.Write(buf.Bytes()) + c.response.Write([]byte(html)) return } -// String formats according to a format specifier and sends text response with status -// code. -func (c *Context) String(code int, format string, a ...interface{}) (err error) { - buf := new(bytes.Buffer) - _, err = fmt.Fprintf(buf, format, a...) - if err != nil { - return err - } +// String sends a string response with status code. +func (c *Context) String(code int, s string) (err error) { c.response.Header().Set(ContentType, TextPlain) c.response.WriteHeader(code) - c.response.Write(buf.Bytes()) + c.response.Write([]byte(s)) return } diff --git a/recipes/file-upload/server.go b/recipes/file-upload/server.go index e01c1fb3..05ba9e99 100644 --- a/recipes/file-upload/server.go +++ b/recipes/file-upload/server.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "io" "os" @@ -39,8 +40,8 @@ func upload(c *echo.Context) error { return err } } - return c.String(http.StatusOK, "Thank You! %s <%s>, %d files uploaded successfully.", - name, email, len(files)) + return c.String(http.StatusOK, fmt.Sprintf("Thank You! %s <%s>, %d files uploaded successfully.", + name, email, len(files))) } func main() { diff --git a/recipes/streaming-file-upload/server.go b/recipes/streaming-file-upload/server.go index 6a826439..98778eec 100644 --- a/recipes/streaming-file-upload/server.go +++ b/recipes/streaming-file-upload/server.go @@ -1,13 +1,15 @@ package main import ( + "fmt" "io/ioutil" - "github.com/labstack/echo" - mw "github.com/labstack/echo/middleware" "io" "net/http" "os" + + "github.com/labstack/echo" + mw "github.com/labstack/echo/middleware" ) func upload(c *echo.Context) error { @@ -63,8 +65,8 @@ func upload(c *echo.Context) error { } i++ } - return c.String(http.StatusOK, "Thank You! %s <%s>, %d files uploaded successfully.", - name, email, i) + return c.String(http.StatusOK, fmt.Sprintf("Thank You! %s <%s>, %d files uploaded successfully.", + name, email, i)) } func main() { diff --git a/website/content/guide/request.md b/website/content/guide/request.md index dadba26b..ccf9bfd5 100644 --- a/website/content/guide/request.md +++ b/website/content/guide/request.md @@ -8,7 +8,7 @@ menu: ### Handler path -`Context#Path()` returns the registered path for a handler, it can be used in the +`Context#Path()` returns the registered path for the handler, it can be used in the middleware for logging purpose. *Example* diff --git a/website/content/guide/response.md b/website/content/guide/response.md index a348c7c5..db48a3a0 100644 --- a/website/content/guide/response.md +++ b/website/content/guide/response.md @@ -89,7 +89,7 @@ Sends an XML HTTP response with status code. Context.HTML(code int, html string) error ``` -Sends an HTML HTTP response with status code. +Sends an HTML response with status code. ### String @@ -97,7 +97,7 @@ Sends an HTML HTTP response with status code. Context.String(code int, s string) error ``` -Sends a text/plain HTTP response with status code. +Sends a string response with status code. ### File