为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我不能使用“文件”作为 http 请求的“正文”?

为什么我不能使用“文件”作为 http 请求的“正文”?

Go
忽然笑 2021-10-25 17:02:20
“http.Post”需要一个“Reader”作为主体参数。“文件”实现“阅读器”。但是如果我将文件作为 body 参数传递,我总是在另一端收到 0 个字节。为什么?这是代码:package mainimport (    "fmt"    "net/http"    "os")func main() {    file, err := os.Open("lala.txt")    if err != nil {        fmt.Printf("file open errrrr %v \n", err)    }    defer file.Close()    resp, err := http.Post("http://requestb.in/11fta851", "text/plain", file)    if err != nil {        fmt.Printf("errrrr %v \n", err)    } else {        fmt.Printf("resp code %d \n", resp.StatusCode)    }}我知道您可以对缓冲区执行“file.ReadAll”并使用它。但感觉像是双重工作。
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 126 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信