使用 python 请求的多部分数据 POST:未找到多部分边界
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17415084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
multipart data POST using python requests: no multipart boundary was found
提问by jeera
I have a form-data as well as file to be sent in the same POST. For ex, {duration: 2000, file: test.wav}. I saw the many threads here on multipart/form-data posting using python requests. They were useful, especially this one.
我有一个表单数据以及要在同一个 POST 中发送的文件。例如,{duration: 2000, file: test.wav}。我在使用 python 请求的 multipart/form-data 发布中看到了许多线程。它们很有用,尤其是这个。
My sample request is as below:
我的示例请求如下:
files = {'file': ('wavfile', open(filename, 'rb'))}
data = {'duration': duration}
headers = {'content-type': 'multipart/form-data'}
r = self.session.post(url, files=files, data=data, headers=headers)
But when I execute the above code, I get this error:
但是当我执行上面的代码时,我得到这个错误:
5:59:55.338 Dbg 09900 [DEBUG] Resolving exception from handler [null]: org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found.
5:59:55.338 Dbg 09900 [DEBUG] 解决处理程序异常 [null]:org.springframework.web.multipart.MultipartException:无法解析多部分 servlet 请求;嵌套异常是 org.apache.commons.fileupload.FileUploadException:请求被拒绝,因为没有找到多部分边界。
So my questions are: 1) How can I see the content of the request being sent? Couldn't use wireshark, its not across the network. 2) why is the boundary missing in the encoded data? Did I miss anything, please point out.
所以我的问题是:1)我如何看到正在发送的请求的内容?无法使用wireshark,它不能跨网络使用。2)为什么编码数据中缺少边界?我有没有遗漏什么,请指出。
采纳答案by Ian Stapleton Cordasco
You should NEVER set that header yourself. We set the header properly with the boundary. If you set that header, we won't and your server won't know what boundary to expect (since it is added to the header). Remove your custom Content-Type header and you'll be fine.
您永远不应该自己设置该标题。我们使用边界正确设置标题。如果您设置该标头,我们将不会并且您的服务器将不知道预期的边界(因为它已添加到标头中)。删除您的自定义 Content-Type 标头,您会没事的。
回答by Anirban Kundu
Taking out the Content-Type header with explicit "multipart/form-data" worked!
使用显式“multipart/form-data”取出 Content-Type 标头工作!
回答by jeet.chanchawat
To specifically add boundary add following in header :
要专门添加边界,请在 header 中添加以下内容:
headers = {
'content-type': 'multipart/form-data; boundary=ebf9f03029db4c2799ae16b5428b06bd'
}