gin icon indicating copy to clipboard operation
gin copied to clipboard

context.BindJSON does not respond with error message

Open jtuchel opened this issue 2 years ago • 3 comments

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

jtuchel avatar Sep 04 '22 17:09 jtuchel

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

Gasoid avatar Sep 04 '22 18:09 Gasoid

Yeah, I have the same problem

tiredsosha avatar Sep 23 '22 11:09 tiredsosha

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

Heliner avatar Oct 31 '22 13:10 Heliner