/
main.go
46 lines (39 loc) · 885 Bytes
/
main.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
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
)
func checkError(err error) {
if err != nil {
panic(err)
}
}
func postFile(filename string, targetUrl string) {
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
fileWriter, err := bodyWriter.CreateFormFile("uploadfile", filename)
checkError(err)
fh, err := os.Open(filename)
checkError(err)
_, err = io.Copy(fileWriter, fh)
checkError(err)
contentType := bodyWriter.FormDataContentType()
bodyWriter.Close()
resp, err := http.Post(targetUrl, contentType, bodyBuf)
checkError(err)
defer resp.Body.Close()
resp_body, err := ioutil.ReadAll(resp.Body)
checkError(err)
fmt.Println(resp.Status)
fmt.Println(string(resp_body))
}
func main() {
target_url := "http://localhost:9090/upload"
filename := "../file.txt"
postFile(filename, target_url)
}