0

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?

marathon
  • 7,881
  • 17
  • 74
  • 137

1 Answers1

0

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)
marathon
  • 7,881
  • 17
  • 74
  • 137