-
Notifications
You must be signed in to change notification settings - Fork 53
/
bodytype.go
39 lines (34 loc) · 979 Bytes
/
bodytype.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
package bodytype
import (
c "gopkg.in/h2non/gentleman.v2/context"
p "gopkg.in/h2non/gentleman.v2/plugin"
"net/http"
)
// Types is a map of MIME type aliases
var Types = map[string]string{
"html": "text/html",
"json": "application/json",
"xml": "application/xml",
"text": "text/plain",
"urlencoded": "application/x-www-form-urlencoded",
"form": "application/x-www-form-urlencoded",
"form-data": "application/x-www-form-urlencoded",
}
// Set sets the Content Type header value, optionally based on a MIME type alias.
func Set(name string) p.Plugin {
return p.NewRequestPlugin(func(ctx *c.Context, h c.Handler) {
defineType(name, ctx.Request)
h.Next(ctx)
})
}
// Type is an alias to Set, which defines the Content-Type header
func Type(name string) p.Plugin {
return Set(name)
}
func defineType(name string, req *http.Request) {
match := Types[name]
if match == "" {
match = name
}
req.Header.Set("Content-Type", match)
}