-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
Use BindJSON multiple times fails #1078
Comments
…/gin#1078; skip logging liveness and readiness
I think this could easily be done by changing line Line 24 in ce670a6
|
Sorry, using |
If
It will solve the issue, but I don't know the method is OK? |
@thinkerou I don't think it can work. Async call will obfuscate variable o. |
@JorritSalverda I think you can read once then set to ctx, and when u need it, just read it from ctx. |
Yes, create a middleware that executes before your request handler. Take your json bytes, and set it in the *gin.Context. When your handler exits, your middlware can continue and you can pull it from the context. |
Any update? I have the same problem |
I have Found this post ,it may help |
@lishuhao yeah, this solved, but maybe it's not the right way. You can't guarantee every middleware using NopCloser to write back. Try to define a []byte in context and reuse it maybe better. |
#1341 should fix it. |
please use |
import ( var s1 MyStruct1 if err := c.ShouldBindBodyWith(&s1, binding.JSON);.. |
When using
BindJSON
multiple times in a single request handler like below it fails the second time because thec.Request.Body
has already been read the first time and can't be read a second time.I do this to be able to log the full structure of a request body coming into my application so I can refine my
RequestBody
struct one property at a time. Also when unMarshalling fails I log the origin body as a string, but using the bodyReadCloser
fails ifBindJSON
already ran.A workaround is to go back to the non-gin way of reading the body first
But that obviously isn't very nice.
According to https://stackoverflow.com/questions/31884093/read-multiple-time-a-reader using a
io.TeeReader
in your code would solve the issue. Is that sensible or too slow? Any other options?The text was updated successfully, but these errors were encountered: