在对Golang中的API调用和端点进行调用时,我将将CSV文件传递为:
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, _ := os.Open("temp.csv")
defer file.Close()
part3,
errFile3 := writer.CreateFormFile("file", filepath.Base("temp.csv"))
_, errFile3 = io.Copy(part3, file)
if errFile3 != nil {
fmt.Println(errFile3)
return
}
_ = writer.Close()
req, _ := http.NewRequest("POST", url, payload)
req.Header.Set("Content-Type", writer.FormDataContentType())
但它正在回归:
File with content-type application/octet-stream is not supported
但是从邮递员那里打同样的电话,它成功了吗?有人面对过这个问题吗?
在Golang文件中是否有将内容类型作为"text/csv“传递的方法?
发布于 2022-06-30 07:26:35
要设置内容类型,直接调用CreatePart方法而不是CreateFormFile助手函数:
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition",`form-data; name="file"; filename="temp.csv"`)
h.Set("Content-Type", "text/csv")
part3, errFile3 := writer.CreatePart(h)
如果不使用字符串文字,则转义字段名和文件名,如问题中所示。有关如何进行转义的示例,请参阅CreateFormFile实现。
https://stackoverflow.com/questions/72818110
复制相似问题