APIs & Integrations

breiko
Member

Upload a file to COS Files API with Node.js (fetch)

I’m having troubles uploading a file with the COS API. This is my code:

var form = new FormData();
var file = fs.createReadStream('file.txt');

form.append('files', file);

fetch('https://api.hubapi.com/filemanager/api/v2/files?hidden=false&overwrite=true',
      {
        method: 'POST',
        headers: {
          'Authorization': 'Bearer ' + hubspotParams['accessToken']
        },
        body: form
      })
      .then(function(res) {
        return res.json();
      }).
      then(function(json){
        console.log(json);
      })

There response is a non valid JSON file with a status 200. Also the file is not uploaded.

5 Replies 5
andre9000
Contributor

Upload a file to COS Files API with Node.js (fetch)

Thank you! I was also stuck for a long time trying to solve it with fetch.

breiko
Member

Upload a file to COS Files API with Node.js (fetch)

I had to give up with node-fetch as I was stuck forever. I switched to Unirest and works perfectly.

unirest.post('https://api.hubapi.com/filemanager/api/v2/files')
.headers({
    'Authorization': 'Bearer ' + hubspotParams['accessToken'],
    'Content-Type': 'multipart/form-data'
  })
  .query({
    'overwrite':'true', // if you want to overwrite the file when it already exists
    'hidden':'false' // if you want the file to be visible in the File Manager
  })
  .field('folder_paths', '...') // if you need to change the upload directory
  .field('file_names','...') // if you need to change the file name
  .attach('file', fs.createReadStream('file.txt')) // Attachment
  .end(function (response) {
    console.log(response.body);
  });
breiko
Member

Upload a file to COS Files API with Node.js (fetch)

I managed to get a different error:

status: 411,
statusText: 'Length Required'

So I have added Content-Length but this brought me back to the 500 error.

var form = new FormData();
  var file = fs.createReadStream('file.txt');
  const stats = fs.statSync('file.txt');
  const fileSizeInBytes = stats.size;

  form.append('files', file);

  fetch('https://api.hubapi.com/filemanager/api/v2/files?hidden=false&overwrite=true',
      {
        method: 'POST',
        headers: {
          'Authorization': 'Bearer ' + hubspotParams['accessToken'],
          'Content-Type': 'multipart/form-data',
          'Content-Length': fileSizeInBytes
        },
        body: form
      })
      .then(function(res) {
        console.log(res);
        return res.json();
      }).
      then(function(json){
        console.log(json);
      })
0 Upvotes
Derek_Gervais
HubSpot Alumni
HubSpot Alumni

Upload a file to COS Files API with Node.js (fetch)

Hi @breiko,

Is the Content Type header set to multipart/form-data?

0 Upvotes
breiko
Member

Upload a file to COS Files API with Node.js (fetch)

Unfortunately I have tried already.

If I set Content-Type to multipart/form-data I receive a 500 error and the message is internal error.

0 Upvotes