mirror of
https://github.com/labstack/echo.git
synced 2025-05-29 23:17:34 +02:00
Refactored media types and headers
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
3b87f90bfd
commit
8ace8e2143
@ -63,14 +63,14 @@ func (c *Context) Render(code int, name string, data interface{}) *HTTPError {
|
|||||||
if c.echo.renderer == nil {
|
if c.echo.renderer == nil {
|
||||||
return &HTTPError{Error: RendererNotRegistered}
|
return &HTTPError{Error: RendererNotRegistered}
|
||||||
}
|
}
|
||||||
c.Response.Header().Set(HeaderContentType, MIMEHTML+"; charset=utf-8")
|
c.Response.Header().Set(ContentType, TextHTML+"; charset=utf-8")
|
||||||
c.Response.WriteHeader(code)
|
c.Response.WriteHeader(code)
|
||||||
return c.echo.renderer.Render(c.Response, name, data)
|
return c.echo.renderer.Render(c.Response, name, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// JSON sends an application/json response with status code.
|
// JSON sends an application/json response with status code.
|
||||||
func (c *Context) JSON(code int, v interface{}) *HTTPError {
|
func (c *Context) JSON(code int, v interface{}) *HTTPError {
|
||||||
c.Response.Header().Set(HeaderContentType, MIMEJSON+"; charset=utf-8")
|
c.Response.Header().Set(ContentType, ApplicationJSON+"; charset=utf-8")
|
||||||
c.Response.WriteHeader(code)
|
c.Response.WriteHeader(code)
|
||||||
if err := json.NewEncoder(c.Response).Encode(v); err != nil {
|
if err := json.NewEncoder(c.Response).Encode(v); err != nil {
|
||||||
return &HTTPError{Error: err}
|
return &HTTPError{Error: err}
|
||||||
@ -80,7 +80,7 @@ func (c *Context) JSON(code int, v interface{}) *HTTPError {
|
|||||||
|
|
||||||
// String sends a text/plain response with status code.
|
// String sends a text/plain response with status code.
|
||||||
func (c *Context) String(code int, s string) *HTTPError {
|
func (c *Context) String(code int, s string) *HTTPError {
|
||||||
c.Response.Header().Set(HeaderContentType, MIMEText+"; charset=utf-8")
|
c.Response.Header().Set(ContentType, TextPlain+"; charset=utf-8")
|
||||||
c.Response.WriteHeader(code)
|
c.Response.WriteHeader(code)
|
||||||
if _, err := c.Response.Write([]byte(s)); err != nil {
|
if _, err := c.Response.Write([]byte(s)); err != nil {
|
||||||
return &HTTPError{Error: err}
|
return &HTTPError{Error: err}
|
||||||
@ -90,7 +90,7 @@ func (c *Context) String(code int, s string) *HTTPError {
|
|||||||
|
|
||||||
// HTML sends a text/html response with status code.
|
// HTML sends a text/html response with status code.
|
||||||
func (c *Context) HTML(code int, html string) *HTTPError {
|
func (c *Context) HTML(code int, html string) *HTTPError {
|
||||||
c.Response.Header().Set(HeaderContentType, MIMEHTML+"; charset=utf-8")
|
c.Response.Header().Set(ContentType, TextHTML+"; charset=utf-8")
|
||||||
c.Response.WriteHeader(code)
|
c.Response.WriteHeader(code)
|
||||||
if _, err := c.Response.Write([]byte(html)); err != nil {
|
if _, err := c.Response.Write([]byte(html)); err != nil {
|
||||||
return &HTTPError{Error: err}
|
return &HTTPError{Error: err}
|
||||||
|
@ -33,7 +33,7 @@ func TestContext(t *testing.T) {
|
|||||||
//------
|
//------
|
||||||
|
|
||||||
// JSON
|
// JSON
|
||||||
r.Header.Set(HeaderContentType, MIMEJSON)
|
r.Header.Set(ContentType, ApplicationJSON)
|
||||||
u2 := new(user)
|
u2 := new(user)
|
||||||
if he := c.Bind(u2); he != nil {
|
if he := c.Bind(u2); he != nil {
|
||||||
t.Errorf("bind %#v", he)
|
t.Errorf("bind %#v", he)
|
||||||
@ -41,7 +41,7 @@ func TestContext(t *testing.T) {
|
|||||||
verifyUser(u2, t)
|
verifyUser(u2, t)
|
||||||
|
|
||||||
// FORM
|
// FORM
|
||||||
r.Header.Set(HeaderContentType, MIMEForm)
|
r.Header.Set(ContentType, ApplicationForm)
|
||||||
u2 = new(user)
|
u2 = new(user)
|
||||||
if he := c.Bind(u2); he != nil {
|
if he := c.Bind(u2); he != nil {
|
||||||
t.Errorf("bind %#v", he)
|
t.Errorf("bind %#v", he)
|
||||||
@ -49,7 +49,7 @@ func TestContext(t *testing.T) {
|
|||||||
// TODO: add verification
|
// TODO: add verification
|
||||||
|
|
||||||
// Unsupported
|
// Unsupported
|
||||||
r.Header.Set(HeaderContentType, "")
|
r.Header.Set(ContentType, "")
|
||||||
u2 = new(user)
|
u2 = new(user)
|
||||||
if he := c.Bind(u2); he == nil {
|
if he := c.Bind(u2); he == nil {
|
||||||
t.Errorf("bind %#v", he)
|
t.Errorf("bind %#v", he)
|
||||||
@ -93,21 +93,21 @@ func TestContext(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// JSON
|
// JSON
|
||||||
r.Header.Set(HeaderAccept, MIMEJSON)
|
r.Header.Set(Accept, ApplicationJSON)
|
||||||
c.Response.committed = false
|
c.Response.committed = false
|
||||||
if he := c.JSON(http.StatusOK, u1); he != nil {
|
if he := c.JSON(http.StatusOK, u1); he != nil {
|
||||||
t.Errorf("json %#v", he)
|
t.Errorf("json %#v", he)
|
||||||
}
|
}
|
||||||
|
|
||||||
// String
|
// String
|
||||||
r.Header.Set(HeaderAccept, MIMEText)
|
r.Header.Set(Accept, TextPlain)
|
||||||
c.Response.committed = false
|
c.Response.committed = false
|
||||||
if he := c.String(http.StatusOK, "Hello, World!"); he != nil {
|
if he := c.String(http.StatusOK, "Hello, World!"); he != nil {
|
||||||
t.Errorf("string %#v", he.Error)
|
t.Errorf("string %#v", he.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTML
|
// HTML
|
||||||
r.Header.Set(HeaderAccept, MIMEHTML)
|
r.Header.Set(Accept, TextHTML)
|
||||||
c.Response.committed = false
|
c.Response.committed = false
|
||||||
if he := c.HTML(http.StatusOK, "Hello, <strong>World!</strong>"); he != nil {
|
if he := c.HTML(http.StatusOK, "Hello, <strong>World!</strong>"); he != nil {
|
||||||
t.Errorf("html %v", he.Error)
|
t.Errorf("html %v", he.Error)
|
||||||
|
33
echo.go
33
echo.go
@ -73,16 +73,25 @@ const (
|
|||||||
// TRACE HTTP method
|
// TRACE HTTP method
|
||||||
TRACE = "TRACE"
|
TRACE = "TRACE"
|
||||||
|
|
||||||
MIMEJSON = "application/json"
|
//-------------
|
||||||
MIMEText = "text/plain"
|
// Media types
|
||||||
MIMEHTML = "text/html"
|
//-------------
|
||||||
MIMEForm = "application/x-www-form-urlencoded"
|
|
||||||
MIMEMultipartForm = "multipart/form-data"
|
|
||||||
|
|
||||||
HeaderAccept = "Accept"
|
ApplicationJSON = "application/json"
|
||||||
HeaderContentDisposition = "Content-Disposition"
|
ApplicationProtobuf = "application/protobuf"
|
||||||
HeaderContentLength = "Content-Length"
|
TextPlain = "text/plain"
|
||||||
HeaderContentType = "Content-Type"
|
TextHTML = "text/html"
|
||||||
|
ApplicationForm = "application/x-www-form-urlencoded"
|
||||||
|
MultipartForm = "multipart/form-data"
|
||||||
|
|
||||||
|
//---------
|
||||||
|
// Headers
|
||||||
|
//---------
|
||||||
|
|
||||||
|
Accept = "Accept"
|
||||||
|
ContentDisposition = "Content-Disposition"
|
||||||
|
ContentLength = "Content-Length"
|
||||||
|
ContentType = "Content-Type"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -138,11 +147,11 @@ func New() (e *Echo) {
|
|||||||
http.Error(c.Response, he.Message, he.Code)
|
http.Error(c.Response, he.Message, he.Code)
|
||||||
})
|
})
|
||||||
e.Binder(func(r *http.Request, v interface{}) *HTTPError {
|
e.Binder(func(r *http.Request, v interface{}) *HTTPError {
|
||||||
ct := r.Header.Get(HeaderContentType)
|
ct := r.Header.Get(ContentType)
|
||||||
err := UnsupportedMediaType
|
err := UnsupportedMediaType
|
||||||
if strings.HasPrefix(ct, MIMEJSON) {
|
if strings.HasPrefix(ct, ApplicationJSON) {
|
||||||
err = json.NewDecoder(r.Body).Decode(v)
|
err = json.NewDecoder(r.Body).Decode(v)
|
||||||
} else if strings.HasPrefix(ct, MIMEForm) {
|
} else if strings.HasPrefix(ct, ApplicationForm) {
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user