-
Notifications
You must be signed in to change notification settings - Fork 25
/
enforce_json.go
35 lines (30 loc) · 922 Bytes
/
enforce_json.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
package middleware
import (
"mime"
"net/http"
ce "github.com/content-services/content-sources-backend/pkg/errors"
"github.com/labstack/echo/v4"
)
const JSONMimeType = "application/json"
func enforceJSONContentTypeSkipper(c echo.Context) bool {
return c.Request().Body == http.NoBody
}
func EnforceJSONContentType(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if enforceJSONContentTypeSkipper(c) {
return next(c)
}
mediatype, _, err := mime.ParseMediaType(c.Request().Header.Get("Content-Type"))
if err != nil {
err = ce.NewErrorResponse(http.StatusUnsupportedMediaType, "Error parsing content type", err.Error())
c.Error(err)
return nil
}
if mediatype != JSONMimeType {
err = ce.NewErrorResponse(http.StatusUnsupportedMediaType, "Incorrect content type", "Content-Type must be application/json")
c.Error(err)
return nil
}
return next(c)
}
}