How to validate header?


I’m a new comers in GO and trying to create an API with echo. So far simple API is working, but I want to validate my API only accept application/json content type.

Actually I already success implementation for this validation, but it’s on every function

 e.GET("/", func(c echo.Context) Errors {
        contentType := c.Request().Header.Get("Content-Type")
        if contentType != "application/json" {
           return c.JSON(http.StatusBadRequest, "incorrect content type")

Can i validate this content type not in every method? In my mind is create my own middleware for this, am i right?

Yes, you need to create a middleware and place in on top of the middleware stack. For reference you can look into