ivpusic / neo

Go Web Framework
http://ivpusic.github.io/neo/
MIT License
420 stars 43 forks source link

ctx.Req.JsonBody return EOF #50

Closed tablecell closed 3 years ago

tablecell commented 5 years ago

client post json {'id':200,'name':"neo"} to route /api want server return same json test code :

package main
import (
    "github.com/ivpusic/neo"
_     "io/ioutil"
    "fmt"
)
func main() {
    app := neo.App()
    app.Get("/", func(ctx *neo.Ctx) (int, error) {
        html:=`<html><head><script src="https://unpkg.com/axios/dist/axios.min.js"></script></head>
<body><script>
    axios.post('/api', {'id':200,'name':"neo"}).then(res => {  console.log(res.data); });
</script>
axios post 
</body><html>
    `

        return 200, ctx.Res.Text(html)
    })

    app.Post("/api", func(ctx *neo.Ctx) (int, error) {

   /*  body,err:=ioutil.ReadAll(ctx.Req.Body)
      fmt.Println(string(body))

    fmt.Println(ctx.Req.JsonBody(ctx.Req.Body))

       if ( nil != err){
            fmt.Println("err")
          return 500,err
       }
  */    
     fmt.Println(ctx.Req.Body)

    return 200,  ctx.Res.Json(ctx.Req.JsonBody(ctx.Req.Body))
    })

        app.Start()
}

body,err:=ioutil.ReadAll(ctx.Req.Body) read json correct but ctx.Req.JsonBody(xxx) return EOF

tablecell commented 3 years ago
type  MyType struct{
    Id  int
    Name string
}

add MyType struct solved