不支持带有Content-Type应用程序/钟表流的文件。在Golang发送CSV文件时返回
在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”中的“文本/csv”传递给文件?
On making an API call to and endpoint in Golang I am passing CSV file as :
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())
But it is returning :
File with content-type application/octet-stream is not supported
But on making the same call from POSTMAN it is succeeding? Has anyone faced this?
Is there any way of passing content type as "text/csv" in Golang for file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要设置内容类型,请直接调用CreatePart方法,而不是CreateFormFile辅助辅助函数:
如果您不使用问题所示的字符串文字,则逃脱字段名称和文件名。有关如何进行逃避的示例,请参见CreateFormFile实现。
To set the content type, call the CreatePart method directly instead of the CreateFormFile helper function:
Escape the field name and file name if you are not using string literals as shown in the question. See the CreateFormFile implementation for an example of how to do the escaping.