简体   繁体   中英

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 -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"

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM