API Summary Report in Python

Hello,
I’m pretty new to coding for APIs so I’m hoping this is a really simple fix. My ultimate goal is to automate the reporting of how many hours have been spent and how many hours are remaining on each of my Clockify Projects, into a 3rd party tool.
I have tried doing some research on the documentation here: https://clockify.me/developers-api and got some great progress. Connecting to the API, pulling my list of projects, and pulling my list of clients. Trying to use the summary reports always ends up with the following error: “{‘code’: 400, ‘message’: ‘Could not parse JSON’}”. My best guess is something I’m passing in the body is formatted incorrectly. Can anyone spot my issue? Please and thanks!

Note: I am passing my workspaceid and API key as variables but these both work perfectly in my other functions. What is new is: the body and passing the body into the requests.post.

def get_summary_forum():
    url = "https://reports.api.clockify.me/v1/workspaces/" + Workspace_id + "/reports/summary"
    headers = {
        'X-Api-Key': API_key,
        "content-type": "application/json"
    }
    body = {
        "dateRangeStart": "2020-05-10T00:00:00",
        "dateRangeEnd": "2022-07-10T23:59:59",
        "summaryFilter": {
            "groups": ["PROJECT"],
            "sortColumn": "GROUP"
            },
        "exportType": "JSON"
        }

    response = requests.post(url, headers=headers, data=body)
    response = json.loads(response.text)
    return response

Thanks to anyone that can assist :slight_smile:

Hello, the issue is that you’ve tried setting the content type within the headers. A better way to do this is by using the json parameter whilst making the request.

Another thing to note is that the module requests has its own way of parsing json data, so instead of using the json module to parse the data you may simply use the .json() function.

def get_summary_forum():
    url = f"https://reports.api.clockify.me/v1/workspaces/{Workspace_id}/reports/summary"
    headers = {'X-Api-Key': API_key}
    data = {
        "dateRangeStart": "2020-05-10T00:00:00",
        "dateRangeEnd": "2022-07-10T23:59:59",
        "summaryFilter": {
            "groups": ["PROJECT"],
            "sortColumn": "GROUP"
            },
        "exportType": "JSON"
        }

    return requests.post(url, headers=headers, json=data).json()
1 Like

Thanks very much for this @BomBomGrest, that fixed it for me! Much appreciated :slight_smile: