diff --git a/content/en/docs/hertz/tutorials/basic-feature/context/request.md b/content/en/docs/hertz/tutorials/basic-feature/context/request.md index b3ea55a11f..65d1e401da 100644 --- a/content/en/docs/hertz/tutorials/basic-feature/context/request.md +++ b/content/en/docs/hertz/tutorials/basic-feature/context/request.md @@ -730,8 +730,12 @@ func (ctx *RequestContext) Body() ([]byte, error) func (ctx *RequestContext) RequestBodyStream() io.Reader func (ctx *RequestContext) MultipartForm() (*multipart.Form, error) func (ctx *RequestContext) PostForm(key string) string +func (ctx *RequestContext) PostFormArray(key string) []string +func (ctx *RequestContext) PostFormMap(key string) map[string][]string func (ctx *RequestContext) DefaultPostForm(key, defaultValue string) string func (ctx *RequestContext) GetPostForm(key string) (string, bool) +func (ctx *RequestContext) GetPostFormArray(key string) ([]string, bool) +func (ctx *RequestContext) GetPostFormMap(key string) map[string][]string func (ctx *RequestContext) PostArgs() *protocol.Args func (ctx *RequestContext) FormValue(key string) []byte func (ctx *RequestContext) SetFormValueFunc(f FormValueFunc) @@ -830,6 +834,54 @@ h.POST("/user", func(ctx context.Context, c *app.RequestContext) { }) ``` +### PostFormArray + +Retrieve `multipart.Form.Value` by name and return all values of the given name. + +> Note: This function supports obtaining values from content-type of application/x-www form urlencoded and multipart/form data, and does not support obtaining file values. + +Function Signature: + +```go +func (ctx *RequestContext) PostFormArray(key string) []string +``` + +Example Code: + +```go +// POST http://example.com/user +// Content-Type: multipart/form-data; +// Content-Disposition: form-data; name="pet"; value="cat" +// Content-Disposition: form-data; name="pet"; value="dog" +h.POST("/user", func(ctx context.Context, c *app.RequestContext) { + pets := c.PostFormArray("pet") // pets == []string{"cat", "dog"} +}) +``` + +### PostFormMap + +Retrieve `multipart.Form.Value` by name and return a map of all values of the given name. + +> Note: This function supports obtaining values from content-type of application/x-www form urlencoded and multipart/form data, and does not support obtaining file values. + +Function Signature: + +```go +func (ctx *RequestContext) PostFormMap(key string) map[string][]string +``` + +Example Code: + +```go +// POST http://example.com/user +// Content-Type: multipart/form-data; +// Content-Disposition: form-data; name="attr[k1]"; value="v1" +// Content-Disposition: form-data; name="attr[k2]"; value="v2" +h.POST("/user", func(ctx context.Context, c *app.RequestContext) { + attrs := c.PostFormMap("attr") // attrs == map[string][]string{"k1": "v1", "k2": "v2"} +}) +``` + ### DefaultPostForm Retrieve `multipart.Form.Value` by name and return the first value of the given name. If it does not exist, return defaultValue. diff --git a/content/zh/docs/hertz/tutorials/basic-feature/context/request.md b/content/zh/docs/hertz/tutorials/basic-feature/context/request.md index 6c3f435bef..2d0b6c62c5 100644 --- a/content/zh/docs/hertz/tutorials/basic-feature/context/request.md +++ b/content/zh/docs/hertz/tutorials/basic-feature/context/request.md @@ -730,8 +730,12 @@ func (ctx *RequestContext) Body() ([]byte, error) func (ctx *RequestContext) RequestBodyStream() io.Reader func (ctx *RequestContext) MultipartForm() (*multipart.Form, error) func (ctx *RequestContext) PostForm(key string) string +func (ctx *RequestContext) PostFormArray(key string) []string +func (ctx *RequestContext) PostFormMap(key string) map[string][]string func (ctx *RequestContext) DefaultPostForm(key, defaultValue string) string func (ctx *RequestContext) GetPostForm(key string) (string, bool) +func (ctx *RequestContext) GetPostFormArray(key string) ([]string, bool) +func (ctx *RequestContext) GetPostFormMap(key string) (map[string][]string, bool) func (ctx *RequestContext) PostArgs() *protocol.Args func (ctx *RequestContext) FormValue(key string) []byte func (ctx *RequestContext) SetFormValueFunc(f FormValueFunc) @@ -830,6 +834,54 @@ h.POST("/user", func(ctx context.Context, c *app.RequestContext) { }) ``` +### PostFormArray + +按名称检索 `multipart.Form.Value`,返回给定 name 的所有值。 + +> 注意:该函数支持从 application/x-www-form-urlencoded 和 multipart/form-data 这两种类型的 content-type 中获取 value 值,且不支持获取文件值。 + +函数签名: + +```go +func (ctx *RequestContext) PostFormArray(key string) []string +``` + +示例: + +```go +// POST http://example.com/user +// Content-Type: multipart/form-data; +// Content-Disposition: form-data; name="pet"; value="cat" +// Content-Disposition: form-data; name="pet"; value="dog" +h.POST("/user", func(ctx context.Context, c *app.RequestContext) { + pets := c.PostFormArray("pet") // pets == []string{"cat", "dog"} +}) +``` + +### PostFormMap + +按名称检索 `multipart.Form.Value`,返回所有值的 map。 + +> 注意:该函数支持从 application/x-www-form-urlencoded 和 multipart/form-data 这两种类型的 content-type 中获取 value 值,且不支持获取文件值。 + +函数签名: + +```go +func (ctx *RequestContext) PostFormMap(key string) map[string][]string +``` + +示例: + +```go +// POST http://example.com/user +// Content-Type: multipart/form-data; +// Content-Disposition: form-data; name="attr[k1]"; value="v1" +// Content-Disposition: form-data; name="attr[k2]"; value="v2" +h.POST("/user", func(ctx context.Context, c *app.RequestContext) { + attrs := c.PostFormMap("attr") // attrs == map[string][]string{"k1": "v1", "k2": "v2"} +}) +``` + ### DefaultPostForm 按名称检索 `multipart.Form.Value`,返回给定 name 的第一个值,如果不存在返回 defaultValue。