forked from mdouchement/standardfile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binder.go
36 lines (29 loc) · 842 Bytes
/
binder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package middlewares
import (
"net/http"
"github.com/labstack/echo/v4"
)
type binder struct {
echo.DefaultBinder
methodsWithBody map[string]bool
}
// NewBinder returns a wrapp of the default binder implementation with extra checks.
func NewBinder() echo.Binder {
return &binder{
methodsWithBody: map[string]bool{
http.MethodPost: true,
http.MethodPatch: true,
http.MethodPut: true,
},
}
}
// Bind implements the echo.Bind interface.
func (b *binder) Bind(i interface{}, c echo.Context) (err error) {
if c.Request().ContentLength == 0 && b.methodsWithBody[c.Request().Method] {
return echo.NewHTTPError(http.StatusBadRequest, "Request body can't be empty")
}
if c.Request().Header.Get("Content-Type") == "" {
c.Request().Header.Set("Content-Type", "application/json")
}
return b.DefaultBinder.Bind(i, c)
}