python-requestsstrava

Strava GPX upload with python module requests is giving a ValueError - why?


I don't understand why this ValueError occurs?

import requests

headers = {'authorization': 'Bearer XXXXXXXXXXXXXXXX'}
files = [{'file': open('../poc_data/Sport-sessions/GPS-data/2012-08-09_05-32-43-UTC_55d566fa93bc7d9d1757b8e0.gpx', 'rb')}, {'data_type': 'gpx'}, {'Content-Type': 'multipart/form-data'}]

r = requests.post('https://www.strava.com/api/v3/uploads', headers=headers, files=files)

Solution

  • The reason for that effect regards to the API-design: it is expecting one explicit dictionary named headers and one named params.
    In my given example above I sent only one (headers) and that explains the ValueError.

    This code works:

    import requests
    
    
    files = {
        'file': open('../poc_data/Sport-sessions/GPS-data/2012-08-09_05-32-43-UTC_55d566fa93bc7d9d1757b8e0.gpx', 'rb')
    }
    headers = {
        'authorization': 'Bearer XXXXXXXXXXXXXXXX'
    }
    params = {
        'data_type': 'gpx'
    }
    url = 'https://www.strava.com/api/v3/uploads'
    
    
    r = requests.post(files=files, headers=headers, params=params, url=url)