TL;DR: Need some help with file encoding for files returned by external API
For my QuickBooks Invoicing Pack, I’m trying to fetch invoice PDFs from the API, and show them to the user (either as a download link like in @Courtney_Milligan1’s Export Tables Pack, or as an attachment column).
The API docs say the invoices/{invoice#}/pdf
endpoint returns a PDF in application/pdf
format.
An example response.body:
%PDF-1.5
4 0 obj
<</Type /Page/Parent 3 0 R/Contents 5 0 R/MediaBox [0 0 612 792]/Resources<</Font<</FAAAAH 7 0 R/FAAAAJ 9 0 R/FAAABC 12 0 R>>/XObject<</X1 14 0 R>>>>/Group <</Type/Group/S/Transparency/CS/DeviceRGB>>>>
endobj
5 0 obj
<</Length 15 0 R/Filter /FlateDecode>>stream
�\b&�FJ��h]#i|N�'7XK�w;��7�}�m��D�Z����[ ��D
...
So I’m taking a hint from the raw image data Packs SDK docs, and structuring things like this with temporaryBlobStorage:
const response = await context.fetcher.fetch({
url: url,
method: "GET",
headers: { Accept: "application/pdf" },
});
let fileBase64 = response.body;
let buffer = Buffer.from(fileBase64, "base64"); // This step doesn't complain
let temporaryFileUrl = await context.temporaryBlobStorage.storeBlob( // This step fails
buffer,
"application/pdf"
);
return temporaryFileUrl;
In the CLI, this executes without error, returning something like https://not-a-real-url.s3.amazonaws.com/tempBlob/1593a504-e08f-4019-88f6-3cc8bfff63d8
When uploaded to Coda, this error gets thrown when we hit the storeBlob()
line:
Error 13 INTERNAL: Request message serialization failure: Failure: Cannot coerce to Uint8Array: object
This feels to me like an issue interpreting the encoding of the API response? I don’t have a lot of experience with this but one thing that’s notable is that the response has a bunch of normal text characters off the top, and eventually gets into unrepresentable (binary?) data. Any thoughts? Is my response really base64 or perhaps something else?