python - HTTP Error 400 when trying to send json data with urllib2 to toggl api -
when using following code connect toggl api connect , authorised fine.
username = token password = 'api_token' url = 'http://www.toggl.com/api/v3/tasks.json' req = urllib2.request(url) auth_string = base64.encodestring('%s:%s' % (username, password)) req.add_header("authorization", "basic %s" % auth_string) f = urllib2.urlopen(req) response = f.read()
when try start new task have send data in json format. test, created json object , added data request object
data = simplejson.dumps({ 'task':{ 'duration': 1, 'billable': true, 'start': '2010-02-12t16:19:45+02:00', 'description': 'this test task', } }) req = urllib2.request(url, data)
but response recieve "urllib2.httperror: http error 400: bad request". can please point me going wrong?
according api documentation, you're missing @ least header. try:
req = urllib2.request(url, data, {"content-type": "application/json"})
if that's not enough, try using urllib.urlencode on data. if that's still not enough, add user:password request, in api example.
Comments
Post a Comment