I'm trying to hit a file upload API with this bit of go code in a cli tool. The post doesn't seem to even hit the server, but this code runs with no error, and the result message is an empty string.
curl works.
Other Posts work.
Any advice?
file, err := os.Open(c.Args().Get(0))
if err != nil {
exitErr(err)
}
defer file.Close()
fmt.Print("About to upload file ", c.Args().Get(0), "
");
res, err := http.Post(fmt.Sprintf("%s/upload", config.Host), "application/octet-stream", file)
if err != nil {
exitErr(err)
}
defer res.Body.Close()
message, err := ioutil.ReadAll(res.Body)
if err != nil {
exitErr(err)
}
fmt.Print("Uploaded result: ", string(message), "
")
Thanks for the pointers. I never thought to check the status code. I was getting a 307 (Temporary Redirect), not 200. My solution is to loop around, following 'location', up to a max redirect count. I've read that the default http client code in go follows redirects, so I'm a little surprised that I have to do this. Both go and http programming are new to me, and I appreciate the help!