feat: improve get post data. (#1920)

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2019-06-02 17:24:41 +08:00 committed by 田欧
parent 4b6df417e4
commit 08b52e5394

View File

@ -63,6 +63,10 @@ type Context struct {
// queryCache use url.ParseQuery cached the param query result from c.Request.URL.Query() // queryCache use url.ParseQuery cached the param query result from c.Request.URL.Query()
queryCache url.Values queryCache url.Values
// formCache use url.ParseQuery cached PostForm contains the parsed form data from POST, PATCH,
// or PUT body parameters.
formCache url.Values
} }
/************************************/ /************************************/
@ -454,16 +458,24 @@ func (c *Context) PostFormArray(key string) []string {
return values return values
} }
// GetPostFormArray returns a slice of strings for a given form key, plus func (c *Context) getFormCache() {
// a boolean value whether at least one value exists for the given key. if c.formCache == nil {
func (c *Context) GetPostFormArray(key string) ([]string, bool) { c.formCache = make(url.Values)
req := c.Request req := c.Request
if err := req.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil { if err := req.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil {
if err != http.ErrNotMultipart { if err != http.ErrNotMultipart {
debugPrint("error on parse multipart form array: %v", err) debugPrint("error on parse multipart form array: %v", err)
} }
} }
if values := req.PostForm[key]; len(values) > 0 { c.formCache = req.PostForm
}
}
// GetPostFormArray returns a slice of strings for a given form key, plus
// a boolean value whether at least one value exists for the given key.
func (c *Context) GetPostFormArray(key string) ([]string, bool) {
c.getFormCache()
if values := c.formCache[key]; len(values) > 0 {
return values, true return values, true
} }
return []string{}, false return []string{}, false