title: gin-gonic mapping request.body values into a struct
body: <p>Am new to the GO programming language. I am building a web server using the gin-gonic framework. I am trying to map the values from req.body onto a struct.</p>
  
  <p>Using Postman I am sending a POST request with the following key/vaules under x-www-form-urlencoded</p>
  
  <p>role : admin</p>
  
  <p>username : adminusername</p>
  
  <p>id: 1</p>
  
  <p>my go code is as follows</p>
  
  <pre><code>package jwtsecuritytoken
  
  import (
      "fmt"
      "github.com/gin-gonic/gin"
  )
  
  type requestBody struct {
      role      string
      username  string    
      id        string
  }
  
  func GenerateToken(c *gin.Context) {
      fmt.Println(c.PostForm("role"))
      var reqBody requestBody
      err := c.Bind(reqBody)
      if err != nil {
          fmt.Println(err)
      }
  
      fmt.Println(reqBody)
      content := gin.H{"Hello": "World"}
      c.JSON(200, content)
     }
  </code></pre>
  
  <p>I am able to read the value from c.PostForm("name")
  but when i try to bind it to the struct I get the following error</p>
  
  <pre><code>reflect: Elem of invalid type
  C:/Go/src/runtime/panic.go:491 (0x42bf80)
      gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
   C:/Go/src/reflect/type.go:955 (0x4bdb19)
      (*rtype).Elem: panic("reflect: Elem of invalid type")
     C:/Learnings/Go/src/github.com/gin-gonic/gin/binding/form_mapping.go:15     (0x8b6b5a)
      mapForm: typ := reflect.TypeOf(ptr).Elem()
       C:/Learnings/Go/src/github.com/gin-gonic/gin/binding/form.go:24    
      formBinding.Bind: if err := mapForm(obj, req.Form); err != nil {
    &lt;autogenerated&gt;:1 (0x8b9298)
    C:/Learnings/Go/src/github.com/gin-gonic/gin/context.go:508 (0x8f3319)
      (*Context).ShouldBindWith: return b.Bind(c.Request, obj)
    C:/Learnings/Go/src/github.com/gin-gonic/gin/context.go:475 (0x8f2e3d)
      (*Context).MustBindWith: if err = c.ShouldBindWith(obj, b); err != nil {
      C:/Learnings/Go/src/github.com/gin-gonic/gin/context.go:458 (0x8f2a7d)
      (*Context).Bind: return c.MustBindWith(obj, b)
      C:/Learnings/Go/src/AuthenticateService/jwtsecuritytoken/jwtsecuritytoken.go:22 (0x905c23)
      GenerateToken: err := c.Bind(reqBody)
    C:/Learnings/Go/src/github.com/gin-gonic/gin/context.go:107 (0x8f10b9)
      (*Context).Next: c.handlers[c.index](c)
    C:/Learnings/Go/src/github.com/gin-gonic/gin/recovery.go:46 (0x9038f0)
      RecoveryWithWriter.func1: c.Next()
    C:/Learnings/Go/src/github.com/gin-gonic/gin/context.go:107 (0x8f10b9)
      (*Context).Next: c.handlers[c.index](c)
    C:/Learnings/Go/src/github.com/gin-gonic/gin/logger.go:83 (0x902c12)
      LoggerWithWriter.func1: c.Next()
    C:/Learnings/Go/src/github.com/gin-gonic/gin/context.go:107 (0x8f10b9)
      (*Context).Next: c.handlers[c.index](c)
    C:/Learnings/Go/src/github.com/gin-gonic/gin/gin.go:352 (0x8fa52c)
      (*Engine).handleHTTPRequest: c.Next()
    C:/Learnings/Go/src/github.com/gin-gonic/gin/gin.go:319 (0x8f9cb1)
      (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
    C:/Go/src/net/http/server.go:2619 (0x66b69a)
       serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
    C:/Go/src/net/http/server.go:1801 (0x6676c3)
      (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
     C:/Go/src/runtime/asm_amd64.s:2337 (0x457e70)
      goexit: BYTE    $0x90   // NOP
  </code></pre>