Problem:
Using method
axios.post()
, successfully upload encoded document contents as ‘base64’ to a remote File Manager. However, the uploaded document is still encoded as ‘base64’ in the remote File Manager.
Could you tell me what is typically required when submitting a method
axios.post()
request to ensure decoding ‘base64’ bytes to a properly formatted document during upload by remote service?
Code:
Typescript code using
axios.post
, uploading in FormData file using ‘base64’ bytes read from TXT file.
const filePath = './files/test.txt';const fileName = Path.parse(filePath).name;const fileExt = Path.parse(filePath).ext.replace('.', '');const fileBytes = fs.readFileSync(filePath, { encoding: 'base64' });const fileContentType = mime.lookup(fileExt) as string;const folderPathHubSpot = 'TEST';const fileNameHubSpot = ${fileName}.${fileExt};const formData = new FormData();formData.append('file', fileBytes, { contentType: fileContentType, filename: Path.join(folderPathHubSpot, fileNameHubSpot),});formData.append('folderPath', folderPathHubSpot);formData.append( 'options', '{"access":"PUBLIC_INDEXABLE","ttl":"P2W","overwrite":false,"duplicateValidationStrategy":"NONE","duplicateValidationScope":"EXACT_FOLDER"}',);formData.append('fileName', fileNameHubSpot);const config = { maxBodyLength: Infinity, headers: { // eslint-disable-next-line @typescript-eslint/naming-convention Authorization: Bearer ${hubspotToken}, 'Content-Encoding': 'base64' // Tried adding to header, did not work either },};const response = await axios.post('https://api.hubapi.com/files/v3/files', formData, config);
Stack Overflow