gin-gonic / gin

Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
https://gin-gonic.com/
MIT License
79.2k stars 8.03k forks source link

Error ShouldBindBodyWith EOF #3208

Open mbaezahuenupil opened 2 years ago

mbaezahuenupil commented 2 years ago

Description

How to reproduce

Middleware

package middleware

import (
    "*/src/models"
    "*/src/util"
    "fmt"
    "net/http"

    "github.com/gin-gonic/gin"
    "github.com/gin-gonic/gin/binding"
)

//BodyValidationCheckReservations
func BodyValidationCheckReservations() gin.HandlerFunc {
    return func(c *gin.Context) {
        var bodySchema models.RequestCheckReservations
        if err := c.ShouldBindBodyWith(&bodySchema, binding.JSON); err != nil {
            util.LOGGER.Error("error parser body CheckReservations MBH ", err.Error())
            fmt.Printf("bodySchema %s\n", bodySchema)

            c.AbortWithStatusJSON(http.StatusBadRequest, ErrorResponse400())
            return
        }
        c.Next()
    }
}

Model

package models

type RequestCheckReservations struct {
    Rut string `json:"rut" binding:"required"`
}

Expectations

curl --location --request POST 'localhost:8080/checkReservations' \
--header 'x-country: data' \
--header 'x-commerce: data' \
--header 'x-chref: data' \
--header 'x-rhsref: data' \
--header 'x-cmref: data' \
--header 'Content-Type: application/json' \
--data-raw '{
    "rut": "123456789-0"
}'
Validations ok!

Actual result

go run .\main.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] POST   /checkReservations/       --> http-adapter/src/controller.(*ControllerStruct).CheckReservationsController-fm (9 handlers)
[GIN-debug] POST   /payReservation/          --> http-adapter/src/middleware.BodyValidationPayReservations.func1 (8 handlers)
INFO[2022-06-20 20:31:51.7665] http-adapterrunning port: 8080, version: 1.0.0 
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Listening and serving HTTP on :8080
[GIN-debug] redirecting request 307: /checkReservations/ --> /checkReservations/
2022/06/20 20:31:59 Respondiendo al cliente ::1 Body: 
ERRO[2022-06-20 20:31:59.6768] error parser body CheckReservations MBH EOF  
bodySchema {}

Environment

0x2d3c commented 2 years ago

Whether the request body has been read

mbaezahuenupil commented 2 years ago

Whether the request body has been read

but, the error is EOF, the validations failing

yeqown commented 2 years ago

@mbaezahuenupil Why not try to print the request body before calling ShouldBindBodyWith which checks the body is not empty? That becomes clear where the EOF comes from.