These are chat archives for gin-gonic/gin

27th
Feb 2019
sgon00
@sgon00
Feb 27 03:17
@Depado thanks a lot for the help. I found the problem. Actually c.GetRawData() works too, the way I print the []byte value is wrong.
Btw, regarding the zero value in json validation, I found many github issues about this. Some people suggest using Field *int and binding:"exists” instead of int and required. I think it’s easier just sending a string “0” instead of int. So I modifed my int to string and then everything work fine.
sgon00
@sgon00
Feb 27 06:05
Hi, I learnt that the request body can only be read once. What if I do c.ShouldBindJSONfirst and bind failed with an error? Can I somehow log the problematic request body? I will not always need the body. When the BindJSON succeeds, it’s ok to ignore the request body. But when it failes, I am wondering if I can log it. Thanks a lot.
Depado
@Depado
Feb 27 09:00
This is kind of a bad practice, if you're expecting an int, expect an int, not a string. The technique with the pointer is a pretty common practice to determine whether a value actually exists and is set, even if it's set to its zero value.
You'll probably need that at some point outside of Gin
Depado
@Depado
Feb 27 09:21
As for your question about BindJSON, displaying the error should tell you what you need to know I think :)
sgon00
@sgon00
Feb 27 10:35
@Depado I just saw your reply. Thanks a lot for the detail explanation.
Btw, regarding BindJSON question, the error does tell me what’s wrong, but it doesn’t tell me what the actual request body is. For instance, the zero int required binding problem. The error will just tell me binding failed because json payload doesn’t provide a number field. But the true story is the value of that number is 0. Without seeing the actual body, sometimes it’s hard to debug.
Depado
@Depado
Feb 27 10:36
Usually when I'm facing something like that I just comment the BindJSON line and dump the request using ioutil.ReadAll ^^ I don't know if there's a simple way of "attempting to bind JSON and if that fails, display the query"
Other than copying the whole body in a byte slice, but that seems dangerous
sgon00
@sgon00
Feb 27 10:37
@Depado ok, got it. Thanks a lot. ^_^
Depado
@Depado
Feb 27 10:37
You're welcome :)