ivpusic / neo

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

invalid character '-' in numeric literal #39

Closed SmarkSeven closed 8 years ago

SmarkSeven commented 8 years ago

parsed := &myType{} err := ctx.Req.JsonBody(parsed) if err != nil { return 200, ctx.Res.Text(err.Error()) } Error:invalid character '-' in numeric literal

ivpusic commented 8 years ago

probably something is wrong with your request. can you please post your json body and your struct fields?

SmarkSeven commented 8 years ago

var ( log = golog.GetLogger("application") )

type myType struct { T string json:"t" }

func main() {

app := neo.App()
app.Use(logger.Log)
app.Use(tryerr.Try)
login := app.Region().Prefix("/i")

login.Post("/weChatLogin", route.WeChatLogin)
login.Post("/test", func(ctx *neo.Ctx) (int, error) {
    parsed := &myType{}
    err := ctx.Req.JsonBody(parsed)
    if err != nil {
        return 200, ctx.Res.Text(err.Error())
    }
    return 200, ctx.Res.Text("OK")
})
app.Start()
SmarkSeven commented 8 years ago

func httpPost() { resp, err := http.Post("http://127.0.01:3000/i/test", "application/x-www-form-urlencoded", strings.NewReader("t=cjb")) if err != nil { fmt.Println(err) }

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
    // handle error
}

fmt.Println(string(body))

} Error:invalid character '=' in literal true (expecting 'r') @ivpusic

ivpusic commented 8 years ago

you are sending application/x-www-form-urlencoded body and trying to parse it as application/json. To parse your body you can use native http Request type. Take a look here https://godoc.org/github.com/ivpusic/neo#Request. You can try using https://golang.org/pkg/net/http/#Request.FormValue, or some community libs to extract form values into struct instance.