How to tell Gin explicitly to bind a struct to the request body and nothing else?

Viewed 57

I'm using the gin framework and the following code works fine for me

import (
    "github.com/gin-gonic/gin"
    "net/http"
)

type RequestBody struct {
    MyRequiredField string `json:"myRequiredField" binding:"required"`
}

func Handle(context *gin.Context) {
    var requestBody RequestBody

    err := context.ShouldBindJSON(&requestBody)

    if err != nil {
        context.JSON(http.StatusBadRequest, err.Error())

        return
    }

    // ...
}

I would like to know how I can tell gin that the given struct must be the request body. It must not search for fields in the route parameters or queries.

So is there a way to be more explicit, e.g.

err := context.BindStructToRequestBody(&requestBody)
0 Answers
Related