-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleResponseHandler.go
82 lines (67 loc) · 1.61 KB
/
SimpleResponseHandler.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"fmt"
"io"
"log"
"net/http"
"net/http/httputil"
"os"
"strconv"
)
func handler(w http.ResponseWriter, r *http.Request) {
dump, _ := httputil.DumpRequest(r, true)
r.ParseForm() // PostForm で値を取得するために必要
fmt.Println(string(dump))
query := r.URL.Query()
fmt.Println("Query", query)
fmt.Println("Post Values", r.PostForm)
name := query.Get("name")
fmt.Fprintf(w, "<html><body>hello world<br>%s</body></html>", name)
}
func multipartHandler(w http.ResponseWriter, r *http.Request) {
dump, _ := httputil.DumpRequest(r, true)
fmt.Println(string(dump))
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusBadRequest)
}
file, _, err := r.FormFile("attachment-file")
if err != nil {
log.Fatal(err)
return
}
defer file.Close()
savePath := "D:/hoge.txt"
saveFile, err := os.Create(savePath)
if err != nil {
log.Fatal(err)
return
}
defer saveFile.Close()
_, err = io.Copy(saveFile, file)
if err != nil {
log.Fatal(err)
return
}
w.WriteHeader(http.StatusCreated)
}
func useCookieHandler(w http.ResponseWriter, r *http.Request) {
dump, _ := httputil.DumpRequest(r, true)
fmt.Println(string(dump))
storeCookie := &http.Cookie{
Name: "COUNT",
Value: "1",
}
cookie, _ := r.Cookie("COUNT")
if cookie != nil {
if i, err := strconv.Atoi(cookie.Value); err == nil {
storeCookie.Value = strconv.Itoa(i + 1)
}
}
http.SetCookie(w, storeCookie)
//COUNT key exsit in Cookie
if cookie != nil {
fmt.Fprintf(w, "<html><body>cookie content is %s</body></html>", cookie.Value)
} else {
fmt.Fprintln(w, "<html><body>no cookie</body></html>")
}
}