Search code examples
pythonquire-api

Problems with POST to Quire API


I have been playing with Quire API using python and while the GET calls work fine, I can't do any successful POST calls. I get 400 error: Bad Request. I would appreciate any hints on what I might be doing wrong.

Below are relevant code snippets:

AUTH_ENDPOINT = 'https://quire.io/oauth/token'
API_ENDPOINT = 'https://quire.io/api'

data = {
    'grant_type' : 'refresh_token',
    'refresh_token' : 'my_refresh_code',
    'client_id' : 'my_client_id',
    'client_secret' : 'my_client_secret'
}

r = requests.post(url=AUTH_ENDPOINT, data=data)
response = json.loads(r.text)
access_token = response['access_token']
headers = {'Authorization' : 'Bearer {token}'.format(token=access_token)}

# This works fine
r = requests.get(url=API_ENDPOINT + '/user/id/me', headers=headers)
user = json.loads(r.text)
print(user)

# This doesn't work
task_oid = 'my_task_oid'

data = {
    'description' : 'Test Comment'
}

r = requests.post(
    url=API_ENDPOINT + '/comment/' + task_oid,
    data=data,
    headers=headers,
)


Solution

  • The answer provided by @cor3000 hinted that post data should be passed as JSON. I tested it out and indeed it works. Here is required modification to the POST reqest:

    r = requests.post(
        url=API_ENDPOINT + '/comment/' + task_oid,
        data=json.dumps(data),
        headers=headers,
    )
    

    Alternatively you can also do:

    r = requests.post(
        url=API_ENDPOINT + '/comment/' + task_oid,
        json=data,
        headers=headers,
    )
    

    More details in requests documentation: https://requests.kennethreitz.org/en/master/user/quickstart/#more-complicated-post-requests