With golang code I have to transfer file to remote service using their api. Their requirement is that request MUST NOT use multipart/form-data. I tried this curl command:
curl -i -X PUT -F [email protected] -H "Content-Type: text/plain" https://url.of.endpoint.com
it doesn't work since it simulates form, but this command:
curl -i -X PUT -T text.txt -H "Content-Type: text/plain" https://url.of.endpoint.com
works perfectly.
How can I translate this curl command to golang code?
You have to create a "PUT" request and set its request body to the contents of the file:
package main
import (
"log"
"net/http"
"os"
)
func main() {
data, err := os.Open("text.txt")
if err != nil {
log.Fatal(err)
}
defer data.Close()
req, err := http.NewRequest("PUT", "http://localhost:8080/test.txt", data)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Content-Type", "text/plain")
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With