Skip to content
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

docs(hertz): Add method PostFormArray to app.RequestContext. #1121

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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。
Expand Down