Search code examples
pythonpostpython-requestswunderlist

Getting {"error":"bad_request"} in a Python wunderlist API Task


I want to create a task in python's wunderlist api, and this is my code:

import requests, json
access_token = "MY TOKEN"
client_id= "My Client ID"
h = {"X-Access-Token": access_token, "X-Client-ID": client_id, "Content-Type": "application/json"
da = {"list_ld": 330478059, "title": "TEST TASK"}
url= "https://a.wunderlist.com/api/v1/tasks"
r= requests.post(url, headers=h, data=da)

But I get "bad_request", why?


Solution

  • The data in da that you send needs to be JSONified. Try dumping it to a string -

    from json import dumps
    r = requests.post(url, headers=h, data=dumps(da))
    

    Alternatively, pass da directly as an argument to json=..., which'd automatically take care of serialisation -

    r = requests.post(url, headers=h, json=da)