-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
form.go
41 lines (36 loc) · 962 Bytes
/
form.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// directive: "form"
// https://ggicci.github.io/httpin/directives/form
package core
import (
"mime/multipart"
)
type DirectvieForm struct{}
// Decode implements the "form" executor who extracts values from
// the forms of an HTTP request.
func (*DirectvieForm) Decode(rtm *DirectiveRuntime) error {
req := rtm.GetRequest()
var form multipart.Form
if req.MultipartForm != nil {
form = *req.MultipartForm
} else {
if req.Form != nil {
form.Value = req.Form
}
}
extractor := &FormExtractor{
Runtime: rtm,
Form: form,
KeyNormalizer: nil,
}
return extractor.Extract()
}
// Encode implements the encoder/request builder for "form" directive.
// It builds the form values of an HTTP request, including:
// - form data
// - multipart form data (file upload)
func (*DirectvieForm) Encode(rtm *DirectiveRuntime) error {
encoder := &FormEncoder{
Setter: rtm.GetRequestBuilder().SetForm,
}
return encoder.Execute(rtm)
}