-
Notifications
You must be signed in to change notification settings - Fork 14
/
client.go
68 lines (59 loc) · 1.33 KB
/
client.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
package main
import (
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
)
const boundary = "d083a547-33d1-41d1-94f7-d15a0f36d795"
func main() {
tr := http.DefaultTransport
client := &http.Client{
Transport: tr,
Timeout: 0,
}
fmt.Println("Set up pipe")
pR, pW := io.Pipe()
go func() {
// Set up multipart body for reading
multipartW := multipart.NewWriter(pW)
fmt.Println("Set up multipart writer")
multipartW.SetBoundary(boundary)
fmt.Println("Set up boundary")
partW, err0 := multipartW.CreateFormFile("fakefield", "fakefilename")
fmt.Println("Set up part writer")
if err0 != nil {
panic("Something is amiss creating a part")
}
connector := io.TeeReader(os.Stdin, partW)
buf := make([]byte, 256)
for {
/* stdin -> connector -> partW -> multipartW -> pW -> pR */
_, err := connector.Read(buf)
if err == io.EOF {
break
}
if err != nil {
fmt.Printf("The error reading from connector: %v", err)
}
}
}()
// Send http request chunk encoding the multipart message
req := &http.Request{
Method: "POST",
URL: &url.URL{
Scheme: "http",
Host: "localhost:3000",
Path: "/upload",
},
ProtoMajor: 1,
ProtoMinor: 1,
ContentLength: -1,
Body: pR,
}
fmt.Printf("Doing request\n")
_, err := client.Do(req)
fmt.Printf("Done request. Err: %v\n", err)
}