我需要将 curl POST 请求转换为 python urllib 或/和 urllib2 POST 请求
那是卷曲:
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"
我的 Python 代码:
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)
这是上升的错误:
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request
但是 cURL 代码运行良好。