APIs & Integrations

vladbesson
Participant

Getting 400 error when trying to upload file to File manager

I am trying to upload file to filemanager but getting 400 error in responce, here is the code (JavaScript/Express.js):

app.post('/hubspot-files-api', upload.any(), function (req, res) {
    var data = new FormData();

    data.append("files", fs.createReadStream(path.join(__dirname, '../files/robots.txt')));
    data.append("folder_ids", "Brand Audit form file");

    axios.post('http://api.hubapi.com/filemanager/api/v2/files?hapikey=key', data, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
    .then((res) => {
      console.log(`statusCode: ${res.statusCode}`)
      res.send('OK');
    })
    .catch((error) => {
      console.error(error.message);
    })
  });

Any ideas what I am doing wrong here?

0 Upvotes
1 Reply 1
Derek_Gervais
HubSpot Alumni
HubSpot Alumni

Getting 400 error when trying to upload file to File manager

Hey @vladbesson ,

 

It's tough to say for sure by just looking at your code, but my initial thought here is that the `folder_ids` field needs to be within a `data` field. Here's the Python example code we provide, and `folder_paths` appears to be within the `data` field:

 

import requests
files = {'files': open('/path/on/your/local-computer/report.xls', 'rb')}
r = requests.post(
    'http://api.hubapi.com/filemanager/api/v2/files?hapikey=demo', 
    data={
        "folder_paths": "folder-in-the-cms-file-manager/subfolder"
         },
    files=files)

https://developers.hubspot.com/docs/methods/files/post_files

0 Upvotes