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

Can't read json raw request #23

Closed
ridwankustanto opened this issue Jan 2, 2020 · 4 comments
Closed

Can't read json raw request #23

ridwankustanto opened this issue Jan 2, 2020 · 4 comments

Comments

@ridwankustanto
Copy link

Description

Version: v2.2.0
Environment: Mac Catalina, go1.13.4
Reproduces: always

Hi,

I tried to handle raw json request with Request.Data["key"], but got nil, first I sent this as my request on postman body raw json:

{
    "name": "John Doe",
    "tags": ["tag1", "tag2"]
}

Then I log print out the result, got this:

map[]

And I also tried the Request.Integer(), Request.Bool() and some of them got error like this:

#Case Request.Integer()
Field \"test\" is not an integer

#Case Request.Bool()
Field \"test\" is not a bool

And this is log on terminal:

2020/01/02 12:16:49 Server is running on port: 8090
2020/01/02 12:16:54 Field "test" is not an integer
2020/01/02 12:16:54 Field "test" is not an integer
goroutine 35 [running]: runtime/debug.Stack(0x10e47ec, 0xc0000b2000, 0x2)
    /usr/local/Cellar/go/1.13.4/libexec/src/runtime/debug/stack.go:24 +0x9d
runtime/debug.PrintStack()
    /usr/local/Cellar/go/1.13.4/libexec/src/runtime/debug/stack.go:16 +0x22
github.com/System-Glitch/goyave/v2.(*Response).Error(0xc00000e0a0, 0x148e4a0, 0xc000020190, 0xc0000c3748, 0x100dbc6)
    /Users/ridwankustanto/go/pkg/mod/github.com/!system-!glitch/goyave/v2@v2.1.0/response.go:118 +0x96
github.com/System-Glitch/goyave/v2.recoveryMiddleware.func1.1(0xc00000e0a0)
    
 /Users/ridwankustanto/go/pkg/mod/github.com/!system-!glitch/goyave/v2@v2.1.0/middleware.go:25 +0x4b
panic(0x148e4a0, 0xc000020190)
    /usr/local/Cellar/go/1.13.4/libexec/src/runtime/panic.go:679 +0x1b2
log.Panicf(0x1549f5c, 0x1c, 0xc0000c3810, 0x1, 0x1)
    /usr/local/Cellar/go/1.13.4/libexec/src/log/log.go:345 +0xc0
github.com/System-Glitch/goyave/v2.(*Request).Integer(...)
    /Users/ridwankustanto/go/pkg/mod/github.com/!system-!glitch/goyave/v2@v2.1.0/request.go:127
karmapala-backend/http/controller/user.Register(0xc00000e0a0, 0xc0002960a0)
    /Users/ridwankustanto/go/src/gitlab.com/ridwankustanto/karmapala-backend/http/controller/user/user.go:24 +0x137
github.com/System-Glitch/goyave/v2.validateRequestMiddleware.func1(0xc00000e0a0, 0xc0002960a0)
    

/Users/ridwankustanto/go/pkg/mod/github.com/!system-!glitch/goyave/v2@v2.1.0/middleware.go:106 +0xa5
karmapala-backend/http/middleware.EnableCORS.func1(0xc00000e0a0, 0xc0002960a0)
    /Users/ridwankustanto/go/src/gitlab.com/ridwankustanto/karmapala-backend/http/middleware/cors.go:26 +0x580
github.com/System-Glitch/goyave/v2.languageMiddleware.func1(0xc00000e0a0, 0xc0002960a0)
    /Users/ridwankustanto/go/pkg/mod/github.com/!system-!glitch/goyave/v2@v2.1.0/middleware.go:139 +0xa9
 github.com/System-Glitch/goyave/v2.parseRequestMiddleware.func1(0xc00000e0a0, 0xc0002960a0)
    /Users/ridwankustanto/go/pkg/mod/github.com/!system-!glitch/goyave/v2@v2.1.0/middleware.go:56 +0x118
github.com/System-Glitch/goyave/v2.recoveryMiddleware.func1(0xc00000e0a0, 0xc0002960a0)
    /Users/ridwankustanto/go/pkg/mod/github.com/!system-!glitch/goyave/v2@v2.1.0/middleware.go:29 +0x77
github.com/System-Glitch/goyave/v2.(*Router).requestHandler(0xc0002545a0, 0x15f2220, 0xc0002c6000, 0xc0002a6200, 0x1561998, 0x0)
    /Users/ridwankustanto/go/pkg/mod/github.com/!system-!glitch/goyave/v2@v2.1.0/router.go:120 +0x147
github.com/System-Glitch/goyave/v2.(*Router).Route.func1(0x15f2220, 0xc0002c6000, 0xc0002a6200)
    /Users/ridwankustanto/go/pkg/mod/github.com/!system-!glitch/goyave/v2@v2.1.0/router.go:55 +0x5a
net/http.HandlerFunc.ServeHTTP(0xc0002547e0, 0x15f2220, 0xc0002c6000, 0xc0002a6200)
    /usr/local/Cellar/go/1.13.4/libexec/src/net/http/server.go:2007 +0x44
github.com/gorilla/mux.(*Router).ServeHTTP(0xc0000d8600, 0x15f2220, 0xc0002c6000, 0xc0002c0000)
    /Users/ridwankustanto/go/pkg/mod/github.com/gorilla/mux@v1.7.3/mux.go:212 +0xe2
net/http.serverHandler.ServeHTTP(0xc000248380, 0x15f2220, 0xc0002c6000, 0xc0002c0000)
    /usr/local/Cellar/go/1.13.4/libexec/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc0002b0000, 0x15f36e0, 0xc0002bc000)
    /usr/local/Cellar/go/1.13.4/libexec/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
    /usr/local/Cellar/go/1.13.4/libexec/src/net/http/server.go:2927 +0x38e

Expected result:
Got value on a specific key.

Anyway, happy new year everyone!! 🎉🎉🎉

@System-Glitch
Copy link
Member

Hello and happy new year!

When sending JSON requests, don't forget the Content-Type: application/json header.

@ridwankustanto
Copy link
Author

Ah, I missed it! Now I got the Request.Data works, thanks!

Aaand how about Request.Integer() and the others Accessors? still can't get the value, I tried add Content-Type: application/json or application/x-www-form-urlencoded, but still no luck.

@System-Glitch
Copy link
Member

System-Glitch commented Jan 2, 2020

When numbers are parsed from a JSON request, they are float64. So you can get it using the Numeric() accessor. You will get an integer if you validate the field using the integer rule.

Accessors won't convert your data, so if your request isn't validated, most of them won't work. From the docs:

You shouldn't use these accessors in middleware because they assume the data has been converted by the validation already.

Can you show me the raw request and a print of request.Data with a bool field please?

@ridwankustanto
Copy link
Author

It was like this:

map[{
    "name": "John Doe",
    "tags": ["tag1", "tag2"],
    "is_active": true,
}:]

And I was trying to send the request using Content-Type: application/x-www-form-urlencoded. I just tried the implementation accessor (Numberic and Bool) with Content-Type: application/json and they both work.

Thank you!

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

2 participants