-
Notifications
You must be signed in to change notification settings - Fork 8k
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
Fix #216: Enable to call binding multiple times in some formats #1341
Changes from 3 commits
3ae520b
e825c37
1e0e4a8
2c82901
edc5f44
e68e6d2
09a2ba4
1cecbd3
4a60eed
bfb9aab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ const ( | |
MIMEPlain = binding.MIMEPlain | ||
MIMEPOSTForm = binding.MIMEPOSTForm | ||
MIMEMultipartPOSTForm = binding.MIMEMultipartPOSTForm | ||
BodyBytesKey = "github.com/gin-gonic/gin/bodyBytes" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I don't understand it's mean, a little surprising. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. fixed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, yes, but it is a bit dangerous. I grepped sources and found There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good, agreed! |
||
) | ||
|
||
const abortIndex int8 = math.MaxInt8 / 2 | ||
|
@@ -508,6 +509,30 @@ func (c *Context) ShouldBindWith(obj interface{}, b binding.Binding) error { | |
return b.Bind(c.Request, obj) | ||
} | ||
|
||
// ShouldBindBodyWith is similar with ShouldBindWith, but it stores the request | ||
// body into the context, and reuse when it is called again. | ||
// | ||
// NOTE: This method reads the body before binding. So you should use | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use two spaces, thanks. |
||
// ShouldBindWith for better performance if you need to call only once. | ||
func (c *Context) ShouldBindBodyWith( | ||
obj interface{}, bb binding.BindingBody, | ||
) (err error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe not need named return. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without this, I should add an explicit declaration // if you use unnamed return value...
func (...) ShouldBindBodyWith(...) error {
...
if body == nil {
// you need explicit declaration here.
var err error
body, err = ioutil.ReadAll(...)
if err != nil {
return err
}
...
}
return ...
} I want to simplify the code and use this named return variable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, got it! Thanks~ |
||
var body []byte | ||
if cb, ok := c.Get(BodyBytesKey); ok { | ||
if cbb, ok := cb.([]byte); ok { | ||
body = cbb | ||
} | ||
} | ||
if body == nil { | ||
body, err = ioutil.ReadAll(c.Request.Body) | ||
if err != nil { | ||
return err | ||
} | ||
c.Set(BodyBytesKey, body) | ||
} | ||
return bb.BindBody(body, obj) | ||
} | ||
|
||
// ClientIP implements a best effort algorithm to return the real client IP, it parses | ||
// X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy. | ||
// Use X-Forwarded-For before X-Real-Ip as nginx uses X-Real-Ip with the proxy's IP. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use two spaces, thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean “one space”, right? mean to replace
...˽Binding.˽˽BindBody˽is...
into...˽Binding.˽BindBody˽is...
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh....so sorry, one space!