xero-node icon indicating copy to clipboard operation
xero-node copied to clipboard

Error during FilesApi.uploadFile()

Open peripat opened this issue 4 years ago • 0 comments

Due to the file size limit with accountingApi.updateInvoiceAttachmentByFileName() I am tyring to switch to using filesApi.uploadFile().

The example given in the documentation uses fs.createReadStream() to generate the body. This isn't possible to use when the file content is not stored in the file system, but is instead stored as a base64 string, but this is the same for updateInvoiceAttachmentByFileName(), which was overcome using the following:

    let buffer = Buffer.from(base64String, 'base64');
    let readStream = new Readable();
    readStream._read = () => {};
    readStream.push(buffer);
    readStream.push(null);
    const contentType = 'application/pdf';
    const options = { headers: { 'Content-Type': contentType } };
    return await XeroClient.accountingApi.updateInvoiceAttachmentByFileName(
      tenantId,
      xeroInvoiceId,
      'myfile.pdf',
      readStream,
      options
    );

This has been working fine for a long time.

If I change the updateInvoiceAttachmentByFileName() line to be:

    return await XeroClient.filesApi.uploadFile(
      tenantId,
      readStream,
      'myfile.pdf',
      contentType,
      options
    );

Then it fails with an error TypeError: source.on is not a function

If I remove the options then the request is made but it returns a 400 status code with Bad Request.

As both the updateInvoiceAttachmentByFileName and uploadFile are expecting a read stream, and I am providing the same value generated from the same PDF file, shouldn't they both accept that field? If not, how else can the read stream be generated given that the fs.ReadStream can only be generated by createReadStream()?

I also tested running uploadFile using the same file from local using createReadStream() and the file uploaded but the filename field was not used on the uploaded file.

Thanks for your help

peripat avatar Oct 15 '21 17:10 peripat