mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-24 02:32:17 +08:00
support post-form map
This commit is contained in:
parent
d84be8c579
commit
3b4c0f1325
25
context.go
25
context.go
@ -439,6 +439,31 @@ func (c *Context) GetPostFormArray(key string) ([]string, bool) {
|
|||||||
return []string{}, false
|
return []string{}, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PostFormMap returns a map for a given form key.
|
||||||
|
func (c *Context) PostFormMap(key string) map[string]string {
|
||||||
|
dicts, _ := c.GetPostFormMap(key)
|
||||||
|
return dicts
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPostFormMap returns a map for a given form key, plus a boolean value
|
||||||
|
// whether at least one value exists for the given key.
|
||||||
|
func (c *Context) GetPostFormMap(key string) (map[string]string, bool) {
|
||||||
|
req := c.Request
|
||||||
|
req.ParseForm()
|
||||||
|
req.ParseMultipartForm(c.engine.MaxMultipartMemory)
|
||||||
|
dicts := make(map[string]string)
|
||||||
|
exist := false
|
||||||
|
for k, v := range req.PostForm {
|
||||||
|
if i := strings.IndexByte(k, '['); i >= 1 && k[0:i] == key {
|
||||||
|
if j := strings.IndexByte(k[i+1:], ']'); j >= 1 {
|
||||||
|
exist = true
|
||||||
|
dicts[k[i+1:][:j]] = v[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dicts, exist
|
||||||
|
}
|
||||||
|
|
||||||
// FormFile returns the first file for the provided form key.
|
// FormFile returns the first file for the provided form key.
|
||||||
func (c *Context) FormFile(name string) (*multipart.FileHeader, error) {
|
func (c *Context) FormFile(name string) (*multipart.FileHeader, error) {
|
||||||
_, fh, err := c.Request.FormFile(name)
|
_, fh, err := c.Request.FormFile(name)
|
||||||
|
@ -47,6 +47,8 @@ func createMultipartRequest() *http.Request {
|
|||||||
must(mw.WriteField("time_local", "31/12/2016 14:55"))
|
must(mw.WriteField("time_local", "31/12/2016 14:55"))
|
||||||
must(mw.WriteField("time_utc", "31/12/2016 14:55"))
|
must(mw.WriteField("time_utc", "31/12/2016 14:55"))
|
||||||
must(mw.WriteField("time_location", "31/12/2016 14:55"))
|
must(mw.WriteField("time_location", "31/12/2016 14:55"))
|
||||||
|
must(mw.WriteField("names[a]", "thinkerou"))
|
||||||
|
must(mw.WriteField("names[b]", "tianou"))
|
||||||
req, err := http.NewRequest("POST", "/", body)
|
req, err := http.NewRequest("POST", "/", body)
|
||||||
must(err)
|
must(err)
|
||||||
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
|
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
|
||||||
@ -540,6 +542,22 @@ func TestContextPostFormMultipart(t *testing.T) {
|
|||||||
values = c.PostFormArray("foo")
|
values = c.PostFormArray("foo")
|
||||||
assert.Equal(t, 1, len(values))
|
assert.Equal(t, 1, len(values))
|
||||||
assert.Equal(t, "bar", values[0])
|
assert.Equal(t, "bar", values[0])
|
||||||
|
|
||||||
|
dicts, ok := c.GetPostFormMap("names")
|
||||||
|
assert.True(t, ok)
|
||||||
|
assert.Equal(t, "thinkerou", dicts["a"])
|
||||||
|
assert.Equal(t, "tianou", dicts["b"])
|
||||||
|
|
||||||
|
dicts, ok = c.GetPostFormMap("nokey")
|
||||||
|
assert.False(t, ok)
|
||||||
|
assert.Equal(t, 0, len(dicts))
|
||||||
|
|
||||||
|
dicts = c.PostFormMap("names")
|
||||||
|
assert.Equal(t, "thinkerou", dicts["a"])
|
||||||
|
assert.Equal(t, "tianou", dicts["b"])
|
||||||
|
|
||||||
|
dicts = c.PostFormMap("nokey")
|
||||||
|
assert.Equal(t, 0, len(dicts))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContextSetCookie(t *testing.T) {
|
func TestContextSetCookie(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user