I'm attempting to upload a gpx file to the uploads API, but keep running into errors.
I am doing this with javascript/nodejs. Unfortunately due to the deprecation of the request-json library, the strava api npm library has broken, so I'm trying to implement this with node-fetch. The code I'm using is:
const content = fs.createReadStream(__dirname + '/tmp/route.gpx');
const fileForm = new FormData();
fileForm.append('file', content, {
"Content-Type": "multipart/form-data",
"filename": "route.gpx"
});
const postBody = {
file: fileForm,
name: title,
description,
data_Type: "gpx",
sport_type: sportType,
external_id: externalId
};
method: 'post',
body: postBody,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${accessToken}`,
}
});
I've tried lots of variations of this including
- JSON.stringify-ing the request body
- Changing content type to 'multipart/form-data'
- setting 'file' directly to the readstream object
Nothing has worked. I usually get one of these three errors:
- {"status":400,"resource":"body","field":"json","code":"invalid encoding"}
- {"message":"Bad Request","errors":[{"resource":"Upload","field":"file","code":"not a file"}]}
- {"message":"Bad Request","errors":[{"resource":"Upload","field":"data","code":"empty"}]}
FWIW I have also manually uploaded the gpx file to strava without issue, so the file data is formatted correctly. I appreciate any help!