0

I'm using Python 3.7. I'm trying to make a CURL POST request using PyCurl , but I'm having trouble figuring out how to assemble the form parameters. I tried this

conn = pycurl.Curl()
if python3:
    conn.setopt(conn.CAINFO, certifi.where())
conn.setopt(conn.URL, str(SEARCH_URL))
send = [("MAX_FILE_SIZE", 10485760),
        ('url', image_url),
        ('search', 'search'),
        ('nsfwfilter', 'on'),]
conn.setopt(pycurl.HTTPPOST, send)
conn.setopt(conn.FOLLOWLOCATION, 1)
conn.setopt(conn.USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0')
conn.setopt(conn.WRITEFUNCTION, returned_code.write)
conn.perform()
conn.close()

This dies with the error

  File "/Users/davea/Documents/workspace/myproject_project/myproject/services/media_service.py", line 71, in doImageSearch
    conn.setopt(pycurl.HTTPPOST, send)
TypeError: unsupported second type in tuple

How do I properly format the parametesr?

Dave
  • 15,639
  • 133
  • 442
  • 830

1 Answers1

1

I posted my comment based on the error message and a hunch (I imagined that MAX_FILE_SIZE is one of the request's (HTTP) headers), and also didn't test it, but apparently I was correct. I tried looking for official documentation to support my statement, but I didn't have much luck.
I searched [PycURL]: Curl Object setopt(option, value) → None, where I found this:

It is possible to set integer options - and only them - that PycURL does not know about by using the numeric value of the option constant directly. For example, pycurl.VERBOSE has the value 42, and may be set as follows:

c.setopt(42, 1)

, but that's not what I'm expecting. Then, going further: [Haxx.cURL]: CURLOPT_HTTPPOST explained, but it doesn't contain much useful info either.
The only place that seems to be remotely related, is the mail thread: [Haxx.cURL]: Difficulty uploading files via http:

I'm attempting to upload a file to a form that looks like this:

<form enctype="multipart/form-data" action="kget.php" method="post">
 <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
 <input name="uploaded_file" type="file" />
 <input type="submit" name="go" value="Upload" />
</form>

So, making MAX_FILE_SIZE's value a string, did the trick:

send = [
    ("MAX_FILE_SIZE", "10485760"),  # @TODO - cfati: Notice the quotes
    ("url", image_url),
    ("search", "search"),
    ("nsfwfilter", "on"),
]
CristiFati
  • 38,250
  • 9
  • 50
  • 87