doumaoao0182 2019-07-30 15:22
浏览 20

http.Post中的行不通吗?

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), "
")
  • 写回答

1条回答 默认 最新

  • duan0513 2019-07-31 10:33
    关注

    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!

    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗