buf := new(bytes.Buffer)
writer := multipart.NewWriter(buf)
formFile, err := writer.CreateFormFile("file", "profile.yml")
_, err = formFile.Write([]byte(file))
if err != nil {
return err
}
//一定要立即关闭,否则数据不会写进去
writer.Close()
req, err := http.NewRequest(transpond.Post, URL, buf)
if err != nil {
return err
}
req.Header.Set("Content-Type", writer.FormDataContentType()) //不能省略,并且一定要先执行writer.Close()
req.Header.Add("X-Session-Token", common.Token)
res, err := http.DefaultClient.Do(req)