Error with uploading activity

168 views
Skip to first unread message

Anish Shourie

unread,
Apr 21, 2021, 6:23:12 AM4/21/21
to Strava API
Hello,

I am getting the following error when trying to upload an activity to the Strava API. 

{'message': 'Bad Request', 'errors': [{'resource': 'Upload', 'field': 'file', 'code': 'not a file'}]}

I have a GPX file that I need to upload. You can also take a look at the code in Python (attached as image). Assume the access token is correct.

- Anish


Screen Shot 2021-04-21 at 5.19.42 AM.png

kingo...@gmail.com

unread,
Apr 21, 2021, 8:13:38 AM4/21/21
to Strava API

As the error message states, the 'file' field you've POSTed does not contain a file, it's just a filename. How does the file contents magically get sent to Strava in your example code?

There's an explicit mention of this in the docs that specifically applies to curl but equally to you here (except just prefixing it with '@' obviously won't work in Python and you need to write more code to read the contents of your file).

> For file, the @ symbol means to use the content of the file at the file path we specify. Otherwise, we are just giving it a string that’s the file name, not the content.

Anish Shourie

unread,
Apr 21, 2021, 2:57:47 PM4/21/21
to Strava API
Thank you for your response! I tried passing the opened file and the contents of the file (file.read()). How exactly would I get the content of the file to pass to the request?

- Anish

Message has been deleted

Walker Sutton

unread,
Sep 19, 2021, 11:59:08 PM9/19/21
to Strava API
Here's a Python solution that worked for me: (I referenced this example: https://github.com/rasapetter/strava-upload/blob/master/strava.py)

files = {
    'file': open('<path_to_file>', 'rb')
}
headers = {
    'Authorization': 'Bearer ' + access_token
}
params = {
    'name': 'name',
    'description': 'description',
    'trainer': 'trainer',
    'commute': 'commute',
    'data_type': 'gpx',
    'external_id': '123456789'
}

url = upload_url
requests.post(files=files, headers=headers, params=params, url=url)

Reply all
Reply to author
Forward
0 new messages