Trouble with pycurl.POSTFIELDS

Matt McCormick picture Matt McCormick · Jan 7, 2010 · Viewed 11.3k times · Source

I'm familiar with CURL in PHP but am using it for the first time in Python with pycurl.

I keep getting the error:

Exception Type:     error
Exception Value:    (2, '')

I have no idea what this could mean. Here is my code:

data = {'cmd': '_notify-synch',
        'tx': str(request.GET.get('tx')),
        'at': paypal_pdt_test
        }

post = urllib.urlencode(data)

b = StringIO.StringIO()

ch = pycurl.Curl()
ch.setopt(pycurl.URL, 'https://www.sandbox.paypal.com/cgi-bin/webscr')
ch.setopt(pycurl.POST, 1)
ch.setopt(pycurl.POSTFIELDS, post)
ch.setopt(pycurl.WRITEFUNCTION, b.write)
ch.perform()
ch.close()

The error is referring to the line ch.setopt(pycurl.POSTFIELDS, post)

Answer

mapcuk picture mapcuk · Apr 21, 2010

I do like that:

post_params = [
    ('ASYNCPOST',True),
    ('PREVIOUSPAGE','yahoo.com'),
    ('EVENTID',5),
]
resp_data = urllib.urlencode(post_params)
mycurl.setopt(pycurl.POSTFIELDS, resp_data)
mycurl.setopt(pycurl.POST, 1)
...
mycurl.perform()