Gin: c.ShouldBindJSON undefined (type *gin.Context has no field or method ShouldBindJSON)

Created on 23 Jan 2018  路  18Comments  路  Source: gin-gonic/gin

  • the err is as below:
    c.ShouldBindJSON undefined (type *gin.Context has no field or method ShouldBindJSON)

  • the code is as below:

func (this *CommonController) SetRepo(c *gin.Context) {
    var json db_models.Repo
    if err := c.ShouldBindJSON(&json); err == nil {
        if json.ParentID == "manu" {
            c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
        } else {
            c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
        }
    } else {
        c.JSON(http.StatusBadRequest, gin.H{"errorformat": err.Error()})
    }

    c.JSON(http.StatusOK, gin.H{
        "RepoID":   "1",
        "RepoName": "capital",
    })
}
  • i have imported "gopkg.in/gin-gonic/gin.v1"

  • how to fix this error?

Most helpful comment

I fixed this by using branch: master in dep, here's my dep entry
[[constraint]]
name = "github.com/gin-gonic/gin"
branch="master"

All 18 comments

whygopkg.in/gin-gonic/gin.v1 cannot use c.ShouldBindJSON?
but why github.com/gin-gonic/gin can use c.ShouldBindJSON?

gopkg.in/gin-gonic/gin.v1 is old and deprecated. You should use the github.com's one.

https://github.com/gin-gonic/gin/issues/960#issuecomment-311540010

794

I'm using dep and still have the same problem.

c.ShouldBindJSON undefined (type *gin.Context has no field or method ShouldBindJSON)

This is the excerpt from Gopkg.toml:

[[constraint]]
  branch = "master"
  name = "github.com/gin-gonic/contrib"

[[constraint]]
  name = "github.com/gin-gonic/gin"
  version = "1.2.0"

and this fromGopkg.lock:

[[projects]]
  name = "github.com/gin-gonic/gin"
  packages = [
    ".",
    "binding",
    "render"
  ]
  revision = "d459835d2b077e44f7c9b453505ee29881d5d12d"
  version = "v1.2"

@himanshub16 same here, but using glide.
Also on build, the following error:

local:project nadilas$ go build
# path/to/project/vendor/github.com/gin-gonic/gin/binding
vendor/github.com/gin-gonic/gin/binding/default_validator.go:33:14: undefined: validator.Config
vendor/github.com/gin-gonic/gin/binding/default_validator.go:34:29: too many arguments in call to validator.New

@himanshub16 @nadilas Try the master branch.

@appleboy thanks, but actually, this also helped: https://github.com/gin-gonic/gin/issues/642#issuecomment-357872871

Hi
What's the solution for this using dep?
I couldn't get the above one to work.

@sidmutha Actually converting the project from glide to dep just works with the following specs:
Gopkg.toml:

[[constraint]]
  name = "github.com/gin-gonic/gin"
  version = "~1.2.0"

Gopkg.lock:

[[projects]]
  name = "gopkg.in/go-playground/validator.v8"
  packages = ["."]
  revision = "5f1438d3fca68893a817e4a66806cea46a9e4ebf"
  version = "v8.18.2"

I just created a new project with the latest Go and Dep and still run into this issue

After some tinkering for some reason this line works:

works

c.ShouldBindWith(&json, binding.JSON)

doesnt work

c.ShouldBindJSON(&json)

@datajohnny What worked for me was:

  • Delete all the Gopkg.* files and the vendor directory (if you have them)
  • Run glide init and don't let glide lock versions for you
    (Say No to Would you like Glide to help you find ways to improve your glide.yaml configuration?)
  • Run glide update
  • Run dep init now. This will init from the glide.yaml file.
  • go build should compile

You can delete the vendor directory and run dep ensure to confirm that it indeed works with dep.

@sidmutha I appreciate it but these seems like an unusual amount of work to do for something that is supposed to be added to the core next version, is it a bug with gin or with dep itself is my question now

@datajohnny I guess it's dep because I faced another issue with a library where my project compiled but didn't work properly when using dep. I deleted it from the vendor directory and performed a plain old go get which made it work.

I fixed this by using branch: master in dep, here's my dep entry
[[constraint]]
name = "github.com/gin-gonic/gin"
branch="master"

This is a problem with this repo and the way dep works. Dep automatically pulls from the latest RELEASE 1.2.0 (commit d459835) in July 2017, not the latest master branch. Because this repo isn't releasing minor versions like 1.2.1, we need to do what @vivekkartha suggested. I would recommend releasing more often for us dep users.

Since dep is on the go roadmap, it makes sense that releasing that targets dep users will be exceedingly beneficial--arguably within a year.

Also facing this issue, it's especially troubling because the docs say it's available.

  • the err is as below:
    c.ShouldBindJSON undefined (type *gin.Context has no field or method ShouldBindJSON)
  • the code is as below:
func (this *CommonController) SetRepo(c *gin.Context) {
  var json db_models.Repo
  if err := c.ShouldBindJSON(&json); err == nil {
      if json.ParentID == "manu" {
          c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
      } else {
          c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
      }
  } else {
      c.JSON(http.StatusBadRequest, gin.H{"errorformat": err.Error()})
  }

  c.JSON(http.StatusOK, gin.H{
      "RepoID":   "1",
      "RepoName": "capital",
  })
}
  • i have imported "gopkg.in/gin-gonic/gin.v1"
  • how to fix this error?
  • just know ,i have the same question,finaly i found my gopath is wrong ,click gin.Context ,find function 'ShouldBind' ,and you will know
Was this page helpful?
0 / 5 - 0 ratings

Related issues

windweller picture windweller  路  20Comments

thinkerou picture thinkerou  路  23Comments

mdsantosdev picture mdsantosdev  路  30Comments

TaceyWong picture TaceyWong  路  24Comments

cachafla picture cachafla  路  33Comments