Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error during FilesApi.uploadFile() #556

Open
peripat opened this issue Oct 15, 2021 · 0 comments
Open

Error during FilesApi.uploadFile() #556

peripat opened this issue Oct 15, 2021 · 0 comments
Assignees

Comments

@peripat
Copy link

peripat commented Oct 15, 2021

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

@sangeet-joy-tw sangeet-joy-tw self-assigned this Feb 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants