我正在尝试发出一个简单PUT的上传文件的请求。http.NewRequest接受body(作为io.Reader)。但是传递os.Fileasbody不起作用,而首先将其读入缓冲区可以解决问题:


file, _ := os.Open(filePath)


// request, _ := http.NewRequest("PUT", myURL, file)

// ^^^ why does this not work???


var buf bytes.Buffer

tee := io.TeeReader(file, &buf)

ioutil.ReadAll(tee)                                 

request, _ := http.NewRequest("PUT", myURL, &buf)   // this works fine


request.Header.Set("Content-Type", "application/octet-stream")

http.DefaultClient.Do(request)


ContentLength标头语义服务器是否依赖?浏览所以我的印象是标题是可选的,这显然不是这里的情况。