auth-api/src/handlers/middlewares.go

21 lines
645 B
Go
Raw Normal View History

2020-12-29 13:46:58 +01:00
package handlers
import (
"github.com/gofiber/fiber/v2"
log "github.com/sirupsen/logrus"
)
// RequireJSON is a middleware that makes sure the request content-type always is application/json (or nothing, defaulting to application/json)
2021-01-02 11:56:24 +01:00
func (h Handlers) RequireJSON(c *fiber.Ctx) error {
2020-12-29 13:46:58 +01:00
c.Accepts("application/json")
contentType := string(c.Request().Header.ContentType())
if contentType != "application/json" && contentType != "" {
log.WithFields(log.Fields{"content-type": contentType}).Debug("Invalid content-type in request")
return c.Status(415).JSON([]ResJSONError{{Error: "Invalid content-type"}})
}
c.Next()
return nil
}