-
Notifications
You must be signed in to change notification settings - Fork 2
Forms
Lucas Menéndez edited this page Jun 24, 2019
·
1 revision
One of the ways to send user data between client and server is via Forms. Forms is a protocol convention that includes a specific formats based on HTTP Content-Type header previusly defined. There are two options:
Content-Type | Raw Body |
---|---|
multipart/form-data |
Content-Disposition: form-data; name="foo" bar |
application/x-www-form-urlencoded |
foo=bar |
After parsing form, its possible to access them using context.Form
(check how to parse params in the following section). context.Form
its a map[string]interface{}
with functions like Exists()
or Get()
that provides secure API.
To use data from Forms, it must first be parsed calling context.ParseForm()
. Then, all the params will be accessible from context.Form
(a map of string
and interface{}
):
/**
Handling:
- GET 'application/x-www-form-urlencoded' request to '/hello' with body 'foo=bar'
- POST 'multipart/form-data' request to '/hello' with body 'Content-Disposition: form-data; name="foo" bar'
*/
if err := ctx.ParseForm(); err != nil {
// catch error
}
foo, _ := ctx.Form.Get("foo") // get foo value safely
fmt.Println(foo) // prints form foo field value
fmt.Println(ctx.Form["foo"]) // or get value like a map