Created
September 14, 2019 12:56
-
-
Save rishav-csenitjsr/c8ae5c2f52fe8911330609bc1b3cb3f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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