Skip to content

Instantly share code, notes, and snippets.

@rishav-csenitjsr
Created September 14, 2019 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rishav-csenitjsr/c8ae5c2f52fe8911330609bc1b3cb3f2 to your computer and use it in GitHub Desktop.
Save rishav-csenitjsr/c8ae5c2f52fe8911330609bc1b3cb3f2 to your computer and use it in GitHub Desktop.
package main
import(
pb "gopkg.in/cheggaaa/pb.v1"
"fmt"
"os"
"log"
"net/http"
"bytes"
"time"
"io"
"io/ioutil"
"mime/multipart"
)
func main() {
path := "path_to/myfile.txt"
file, err := os.Open(path)
if err != nil {
log.Fatal("Error")
}
defer file.Close()
body := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(body)
fileWriter, err := bodyWriter.CreateFormFile("file", path)
if err != nil {
log.Fatal("Error")
}
io.Copy(fileWriter, file)
bodyWriter.Close()
bar := pb.New(body.Len()).SetUnits(pb.U_BYTES).SetRefreshRate(time.Millisecond * 10)
bar.ShowSpeed = true
bar.Start()
xx:=bar.NewProxyReader(body)
req, e := http.NewRequest("POST", "https://xyz.com/upload", xx)
if e != nil {
log.Fatal("Request Error")
}
req.Header.Add("Content-Type", bodyWriter.FormDataContentType())
client := &http.Client{}
resp, e := client.Do(req)
bar.Finish()
if e != nil {
log.Fatal("Response Error")
}
respBody, e := ioutil.ReadAll(resp.Body)
fmt.Println(string(respBody))
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment