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

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

问题描述

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

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天全站免登陆