Golang multipart/form-data File Upload

December 8, 2015 ยท View on GitHub

package main

import ( "net/http" "os" "bytes" "path" "path/filepath" "mime/multipart" "io" )

func main() { fileDir, _ := os.Getwd() fileName := "upload-file.txt" filePath := path.Join(fileDir, fileName)

file, _ := os.Open(filePath) defer file.Close()

body := &bytes.Buffer{} writer := multipart.NewWriter(body) part, _ := writer.CreateFormFile("file", filepath.Base(file.Name())) io.Copy(part, file) writer.Close()

r, _ := http.NewRequest("POST", "http://example.com", body) r.Header.Add("Content-Type", writer.FormDataContentType()) client := &http.Client{} client.Do(r) }