[英]from curl to pycurl - how to make a mutli-part post - works with curl, fails with 422 with pycurl
I have a cURL post that works fine: 我有一个很好的cURL帖子:
curl -X POST http://some-server.com/working_endpoint-F "package[distro_version_id]=1" -F "package[package_file]=@/tmp/myfile.bin"
When I try to translate this into pycurl, the request fails with 422 Unprocessable Entity, with the server saying package[package_file]
"must be multipart form-data" 当我尝试将其转换为pycurl时,请求失败,并显示422 Unprocessable Entity,服务器说
package[package_file]
“必须是多部分表单数据”
import pycurl
c = pycurl.Curl()
c.setopt(pycurl.VERBOSE, 1)
c.setopt(c.URL, 'http://some-server.com/working_endpoint')
c.setopt(c.POST, 1)
c.setopt(c.HTTPPOST, [('package[package_file]', (c.FORM_FILE, '/tmp/myfile.bin'))])
c.setopt(c.HTTPPOST, [('package[distro_version_id]', '1')])
c.perform()
Indeed the headers look like only the one parameter is going into the multipart form 实际上,标头看起来像只有一个参数进入多部分形式
Content-Length: 165 Content-Type: multipart/form-data; boundary=------------------------dee07c93fad525aa
What am I doing wrong? 我究竟做错了什么?
Figured it out. 弄清楚了。
Instead of separate setopt calls for the form data, like this 而不是像这样对表单数据进行单独的setopt调用
c.setopt(c.HTTPPOST, [('package[package_file]', (c.FORM_FILE, '/tmp/myfile.bin'))])
c.setopt(c.HTTPPOST, [('package[distro_version_id]', '1')])
It needs to be together in a single structure, like this 它需要像这样一个单一的结构
data = [
('package[distro_version_id]', '1'),
('package[package_file]', (
c.FORM_FILE, '/tmp/myfile.bin,
c.FORM_CONTENTTYPE, 'application/octet-stream'
))]
c.setopt(c.HTTPPOST, data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.