Reputation: 21
I need to transform curl POST request into python urllib or/and urllib2 POST request
THATS CURL:
curl -k -X POST "https://api.foursquare.com/v2/checkins/add" \
-H "Accept-Language: en-US" -H "Accept-Encoding: gzip" \
-H "Connection: Keep-Alive" -H "Content-Type: application/x-www-form-urlencoded" \
--user-agent "com.foursquare.android:2013040200:20130402:4.2.2:GT-I9100:release" \
--data "ll=55.828299%2C49.081782&llAcc=56.0&venueId=5192088a498edf76505871fe&shout=&broadcast=public&oauth_token=blahblahblah&v=blablabla"
MY PYTHON CODE:
import urllib
import urllib2
url = 'https://api.foursquare.com/v2/checkins/add'
values = {
'll' : '55.828299%2C49.081782',
'llAcc' : '56.0',
'venueId' : '5192088a498edf76505871fe',
'shout' : '',
'broadcast' : 'public',
'oauth_token' : 'blahblahblah',
'v' : 'blablabla',
}
data = urllib.urlencode(values)
req = urllib2.Request(url, data) # POST request doesn't not work
req.add_header("Accept-Language", "en-US")
req.add_header("Accept-Encoding", "gzip")
req.add_header("Connection", "Keep-Alive")
req.add_header("Content-Type", "application/x-www-form-urlencoded")
req.add_header("User-Agent","com.foursquare.android:2013040200:20130402:4.2.2:GT-I9100:release")
urllib2.urlopen(req)
That's rising error:
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request
But cURL code working well.
Upvotes: 1
Views: 5690