使用 python 请求的多部分数据 POST:未找到多部分边界 [英] multipart data POST using python requests: no multipart boundary was found

查看:45
本文介绍了使用 python 请求的多部分数据 POST:未找到多部分边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单数据以及要在同一个 POST 中发送的文件.例如,{duration: 2000, file: test.wav}.我在使用 python 请求的 multipart/form-data 发布中看到了许多线程.它们很有用,尤其是这个.

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.

我的示例请求如下:

    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] 正在解析处理程序的异常 [null]:org.springframework.web.multipart.MultipartException:无法解析多部分 servlet 请求;嵌套异常是 org.apache.commons.fileupload.FileUploadException:请求被拒绝,因为没有找到多部分边界.

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.

所以我的问题是:1)我怎样才能看到正在发送的请求的内容?无法使用wireshark,它不能跨网络使用.2)为什么编码数据中缺少边界?有没有遗漏什么,请指出.

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.

推荐答案

您永远不应该自己设置该标题.我们使用边界正确设置标题.如果您设置该标头,我们将不会并且您的服务器将不知道预期的边界(因为它已添加到标头中).删除您的自定义 Content-Type 标头,您会没事的.

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.

这篇关于使用 python 请求的多部分数据 POST:未找到多部分边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆