Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save johnteee/057276642ebbccd1440de5f270027add to your computer and use it in GitHub Desktop.
Save johnteee/057276642ebbccd1440de5f270027add to your computer and use it in GitHub Desktop.

Revisions

  1. @andrewmilson andrewmilson renamed this gist Dec 8, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @andrewmilson andrewmilson created this gist Dec 8, 2015.
    31 changes: 31 additions & 0 deletions uploader.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    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)
    }