mholt / curl-to-go

Convert curl commands to Go code in your browser

Home Page:https://mholt.github.io/curl-to-go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

support --upload-file

EugenMayer opened this issue · comments

That would be fairly awesome!

So what's the equivalent Go for curl -T file.txt ...? Is this it?

f, err := os.Open("file.txt")
if err != nil {
	// handle err
}
defer f.Close()
req, err := http.NewRequest("PUT", "https://example.com/upload", f)
if err != nil {
	// handle err
}
...

I am not sure i ever did any multipart upload with go yet.

Still standing today:

// Generated by curl-to-Go: https://mholt.github.io/curl-to-go

// curl --upload-file ./hello.txt https://transfer.sh/hello.txt

resp, err := http.Get("https://transfer.sh/hello.txt")
if err != nil {
	// handle err
}
defer resp.Body.Close()

How is it going? Where do I start?

How is it that different from ↴ ?

// Generated by curl-to-Go: https://mholt.github.io/curl-to-go

// curl https://api.example.com/upload \
//  --user api:YOUR_API_KEY \
//  --data-binary @file.jpg \
//  --dump-header apiresponse.txt
//

f, err := os.Open("file.jpg")
if err != nil {
	// handle err
}
defer f.Close()
req, err := http.NewRequest("POST", "https://api.example.com/upload", f)
if err != nil {
	// handle err
}
req.SetBasicAuth("api", "YOUR_API_KEY")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

resp, err := http.DefaultClient.Do(req)
if err != nil {
	// handle err
}
defer resp.Body.Close()