Reputation: 2976
I'm really new in Go and I have to integrate Zamzar in a Go microservice. I need to POST
a file and a data type (string).
Doing a curl
looks like this:
curl https://sandbox.zamzar.com/v1/jobs \
-u user:pass \
-X POST \
-F "source_file=@/tmp/portrait.gif" \
-F "target_format=png"
This is what I have so far:
client := &http.Client{}
req, err := http.NewRequest("GET", "https://sandbox.zamzar.com/v1/jobs", nil)
req.SetBasicAuth("user", "pass")
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Error : %s", err)
} else {
fmt.Println(resp)
}
How can I send the target_format
as a string and source_file
as a file? I already have the file ([]byte
)
Upvotes: 2
Views: 1834
Reputation: 120941
Use multipart.Writer to create the request body:
var buf bytes.Buffer
mpw := multipart.NewWriter(&buf)
w, err := mpw.CreateFormFile("source_file", "portrait.gif")
if err != nil {
// handle error
}
if _, err := w.Write(imageBytes); err != nil {
// handle error
}
if err := mpw.WriteField("target_format", "png"); err != nil {
// handle error
}
if err := mpw.Close(); err != nil {
// handle error
}
req, err := http.NewRequest("GET", "https://sandbox.zamzar.com/v1/jobs", &buf)
req.Header.Set("Content-Type", mpw.FormDataContentType())
... continue as before.
Upvotes: 2