Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

context.BindJSON does not respond with error message #3310

Open
jtuchel opened this issue Sep 4, 2022 · 3 comments
Open

context.BindJSON does not respond with error message #3310

jtuchel opened this issue Sep 4, 2022 · 3 comments

Comments

@jtuchel
Copy link

jtuchel commented Sep 4, 2022

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
@Gasoid
Copy link

Gasoid commented Sep 4, 2022

Looks like it is somehow connected to issue #622
#662
BindJSON returns "EOF"
Screenshot 2022-09-04 at 20 26 40

@tiredsosha
Copy link

Yeah, I have the same problem

@Heliner
Copy link
Contributor

Heliner commented Oct 31, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants