-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
63 lines (51 loc) · 1.88 KB
/
http.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package gokits
import (
"io/ioutil"
"net/http"
)
func FormIntValue(request *http.Request, key string) int {
return FormIntValueDefault(request, key, 0)
}
func FormIntValueDefault(request *http.Request, key string, defaultValue int) int {
formValue := request.FormValue(key)
if intValue, err := IntFromStr(formValue); nil == err {
return intValue
}
return defaultValue
}
func RequestBody(request *http.Request) (string, error) {
bytes, err := ioutil.ReadAll(request.Body)
if nil != err {
return "", err
}
return string(bytes), nil
}
func ResponseJson(writer http.ResponseWriter, json string) {
ResponseContent(writer, json, "application/json", "UTF-8")
}
func ResponseText(writer http.ResponseWriter, text string) {
ResponseContent(writer, text, "text/plain", "UTF-8")
}
func ResponseHtml(writer http.ResponseWriter, html string) {
ResponseContent(writer, html, "text/html", "UTF-8")
}
func ResponseContent(writer http.ResponseWriter,
content, contentType, characterEncoding string) {
writer.Header().Set("Content-Type",
contentType+"; charset="+characterEncoding)
_, _ = writer.Write([]byte(content))
}
func ResponseErrorJson(writer http.ResponseWriter, statusCode int, json string) {
ResponseErrorContent(writer, statusCode, json, "application/json", "UTF-8")
}
func ResponseErrorText(writer http.ResponseWriter, statusCode int, text string) {
ResponseErrorContent(writer, statusCode, text, "text/plain", "UTF-8")
}
func ResponseErrorHtml(writer http.ResponseWriter, statusCode int, html string) {
ResponseErrorContent(writer, statusCode, html, "text/html", "UTF-8")
}
func ResponseErrorContent(writer http.ResponseWriter, statusCode int,
errorContent, contentType, characterEncoding string) {
writer.WriteHeader(statusCode)
ResponseContent(writer, errorContent, contentType, characterEncoding)
}