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
76.77k stars 7.92k forks source link

context.BindJSON does not respond with error message #3310

Open jtuchel opened 1 year ago

jtuchel commented 1 year ago

Description

Based on the docs https://github.com/gin-gonic/gin#model-binding-and-validation I tried to use context.BindJSON instead of c.ShouldBindJSON. Unfortunately only c.ShouldBindJSON responds with the error message.

How to reproduce

package main

import (
  "net/http"

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

type MyStruct struct {
    AField string `json:"aField" binding:"required"`
}

func main() {
  r := gin.Default()

  // This one only responds with a 400
  r.POST("/foo", func(c *gin.Context) {
    var ms MyStruct

    err := c.BindJSON(&ms)

    if err != nil {
        return
    }

    c.JSON(http.StatusOK, "passes")
  })

  // This one responds with the error message
  r.POST("/bar", func(c *gin.Context) {
    var ms MyStruct

    err := c.ShouldBindJSON(&ms)

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

        return
    }

    c.JSON(http.StatusOK, "passes")
  })

  r.Run()
}

Expectations

Using Postman I would expect the following result from the endpoint /bar

image

Actual result

Instead I get a 400 but no error message

image

Environment

Gasoid commented 1 year ago

Looks like it is somehow connected to issue #622 https://github.com/gin-gonic/gin/issues/662 BindJSON returns "EOF"

Screenshot 2022-09-04 at 20 26 40
tiredsosha commented 1 year ago

Yeah, I have the same problem

Heliner commented 1 year ago

Description

Based on the docs https://github.com/gin-gonic/gin#model-binding-and-validation I tried to use context.BindJSON instead of c.ShouldBindJSON. Unfortunately only c.ShouldBindJSON responds with the error message.

How to reproduce

package main

import (
  "net/http"

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

type MyStruct struct {
    AField string `json:"aField" binding:"required"`
}

func main() {
  r := gin.Default()

  // This one only responds with a 400
  r.POST("/foo", func(c *gin.Context) {
    var ms MyStruct

    err := c.BindJSON(&ms)

    if err != nil {
        return
    }

  c.JSON(http.StatusOK, "passes")
  })

  // This one responds with the error message
  r.POST("/bar", func(c *gin.Context) {
    var ms MyStruct

    err := c.ShouldBindJSON(&ms)

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

        return
    }

  c.JSON(http.StatusOK, "passes")
  })

  r.Run()
}

Expectations

Using Postman I would expect the following result from the endpoint /bar

image

Actual result

Instead I get a 400 but no error message

image

Environment

* go version: 1.18

* gin version (or commit ref): 1.8.1

* operating system: Linux

try this

    r.POST("/bar", func(c *gin.Context) {
        var ms MyStruct

        err := c.ShouldBindJSON(&ms)

        if err != nil {
            c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
                        // gin.H{}
            //c.JSON(http.StatusBadRequest, err.Error())

            return
        }

        c.JSON(http.StatusOK, "passes")
    })

same with the doc : https://github.com/gin-gonic/gin#model-binding-and-validation