i,m using a lambda function to send clients side uploaded files to the hubspot api. On the client side I encode the file to base64 and then invoke the lambda function with the data. I,m trying to upload the base64 Encoded file, but I can’t find the proper way to pass the file or what the api expects. I was only able to find this line:
““file”: {binary data}”
The error I receive is:
Error: Request failed with status code 400
I think the only thing that goes wrong is passing the file, but i dont know what format the api expects because the documentation are not that clear. Also the error response isn’t very helpfull either.
- Are all the values of the var options correctly filled in?
- How to correctly pass the file file: Buffer.from(event.data.drawing, ‘base64’),?
- Is there any way to get more helpfull axios error responses or debugging advice?
DOCS: Accounts Dashboard | HubSpot
'use strict';
const axios = require("axios");
exports.handler = async (event, context) => {
const apiKey = "myApiKey";
//event.data.drawing the BASE64 encoded string
var options = {
method: 'POST',
baseURL: 'https://api.hubapi.com',
url: '/files/v3/files',
params: { hapikey: apiKey },
headers: { accept: 'application/json', 'content-type': 'multipart/form-data' },
data: {
options: {
access: "PRIVATE",
ttl: 'P2W',
overwrite: false,
duplicateValidationStrategy: 'NONE',
duplicateValidationScope: 'NONE',
},
folderPath: 'offerte_aanvragen',
file: Buffer.from(event.data.drawing, 'base64'),
},
responseType: 'json',
};
return new Promise((resolve, reject) => {
axios(options).then((response) => {
if (response.statusText !== 'OK') {
return reject(response);
} else {
resolve(response)
}
}).catch(error => {
reject(error);
console.log(error)
});
})
}
Source:
https://stackoverflow.com/questions/68378935/how-to-upload-a-file-using-hubspot-file-uploading-api-with-nodejs-axios-aws-lam
I,m having a hard time figuring out what goes wrong or in what format I need to pass the file.
Thanks in advance!