cancel
Showing results for 
Search instead for 
Did you mean: 

invalid encoding when posting upload

arobbins530
Shkhara
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
  };

  const postResponse = await fetch('https://www.strava.com/api/v3/uploads', {
    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:

  1. {"status":400,"resource":"body","field":"json","code":"invalid encoding"}
  2. {"message":"Bad Request","errors":[{"resource":"Upload","field":"file","code":"not a file"}]}
  3. {"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!
1 ACCEPTED SOLUTION

arobbins530
Shkhara

Wow, ok I've been working on this for hours and I solved it minutes after posting this. Here is the code that worked

const content = fs.createReadStream(__dirname + '/tmp/route.gpx');

const fileForm = new FormData();
fileForm.append('file', content, {
  "Content-Type": "multipart/form-data",
  "filename": "route.gpx"
});
fileForm.append("name", title);
fileForm.append("description", description);
fileForm.append("data_type", "gpx");
fileForm.append("sport_type", sportType);
fileForm.append("external_id", externalId);
fileForm.append("Content-Type", "application/octet-stream");
fileForm.append("Content-Type", "multipart/form-data");

const postResponse = await fetch('https://www.strava.com/api/v3/uploads', {
  method: 'post',
  body: fileForm,
  headers: {
    'Authorization': `Bearer ${accessToken}`,
  }
});

 

View solution in original post

2 REPLIES 2

arobbins530
Shkhara

Wow, ok I've been working on this for hours and I solved it minutes after posting this. Here is the code that worked

const content = fs.createReadStream(__dirname + '/tmp/route.gpx');

const fileForm = new FormData();
fileForm.append('file', content, {
  "Content-Type": "multipart/form-data",
  "filename": "route.gpx"
});
fileForm.append("name", title);
fileForm.append("description", description);
fileForm.append("data_type", "gpx");
fileForm.append("sport_type", sportType);
fileForm.append("external_id", externalId);
fileForm.append("Content-Type", "application/octet-stream");
fileForm.append("Content-Type", "multipart/form-data");

const postResponse = await fetch('https://www.strava.com/api/v3/uploads', {
  method: 'post',
  body: fileForm,
  headers: {
    'Authorization': `Bearer ${accessToken}`,
  }
});

 

Sorry, I guess I can't edit. Accidentally left in two lines that should be left out. Remove:

fileForm.append("Content-Type""application/octet-stream");
fileForm.append("Content-Type""multipart/form-data");
Ready, Get Set, Go!

Welcome to the Community - here is your guide to help you get started!


Know how to use Community


Understand Community Settings