-
Notifications
You must be signed in to change notification settings - Fork 0
/
multipart.go
67 lines (57 loc) · 1.48 KB
/
multipart.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
package snippets
import (
"io/ioutil"
"log"
"mime"
"mime/multipart"
"net/http"
)
func getMultipartValues() (ret [][]byte) {
// TODO: just placeholder
return
}
// handler
func handleMultipart(w http.ResponseWriter, r *http.Request) {
mediatype, _, err := mime.ParseMediaType(r.Header.Get("Accept"))
if err != nil {
http.Error(w, err.Error(), http.StatusNotAcceptable)
return
}
if mediatype != "multipart/form-data" {
http.Error(w, "set Accept: multipart/form-data", http.StatusMultipleChoices)
return
}
mw := multipart.NewWriter(w)
w.Header().Set("Content-Type", mw.FormDataContentType())
for _, value := range getMultipartValues() {
fw, err := mw.CreateFormField("value")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err := fw.Write(value); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
if err := mw.Close(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// client side
func requestgetMultipart() {
req, _ := http.NewRequest("GET", "http://localhost:8080/foo", nil)
req.Header.Set("Accept", "multipart/form-data; charset=utf-8")
resp, _ := http.DefaultClient.Do(req)
_, params, _ := mime.ParseMediaType(resp.Header.Get("Content-Type"))
mr := multipart.NewReader(resp.Body, params["boundary"])
for {
part, err := mr.NextPart()
if err != nil {
break
}
value, _ := ioutil.ReadAll(part)
log.Printf("Value: %s", value)
}
}