I am experiencing an issue when trying to download a binary file (PDF) using a Weweb collection. The API I am using correctly returns binary data, but it appears that Weweb converts it to a string, causing the file to become corrupted when downloaded.
Additionally, this file is only available through an endpoint that requires authentication. When trying to fetch the file via JavaScript inside Weweb, I run into CORS issues.
Issue Description:
-
I am fetching a binary file (PDF) from an API that requires authentication .
-
The API correctly returns binary data (tested in Postman, where the file downloads correctly).
-
When I try to store the response in a Weweb collection, it appears that binary data is converted into a string.
-
When I attempt to download the file from Weweb, it becomes corrupted and cannot be opened. I have tried many different ways to convert it back to binary, nothing works.
-
Fetching the file using fetch() in JavaScript directly (inside Weweb) results in a CORS issue.
What I Have Tried:
-
Fetching the file in Postman â
Works fine (file downloads correctly).
-
Fetching the file using fetch() in JavaScript (outside Weweb) â
Blocked by CORS issue .
-
Fetching it through Wewebâs collection â
File gets converted to a string , leading to corruption.
-
- Tried converting the string back to binary in JavaScript â
No success.
- Tried converting the string back to binary in JavaScript â
My Questions:
-
How does Weweb handle binary file responses in collections? Does Weweb automatically convert binary data to a string? If so, is there a way to prevent this? How can I correctly store and retrieve binary files using Wewebâs collections?
-
Is there a recommended way to bypass the CORS issue when fetching this file from an authenticated API?**
Any guidance on resolving this issue would be greatly appreciated. Thank you in advance for your help!
Jan, Chorizo Lab
Below is an example of Javascript code that I use to covert string to binary (not succesful)
const binaryData = collections[/* GET Attachment/file */ 'xxxxxxxxxxx']?.['d\ta']; // Adjust path as needed
console.log(binaryData);
if (binaryData) {
// Convert text-based binary string back to proper binary
function fixBinaryString(str) {
const encoder = new TextEncoder();
return encoder.encode(str); //
}
// Convert Weweb binary data to a proper PDF Blob
const byteArray = fixBinaryString(binaryData);
const pdfBlob = new Blob([byteArray], { type: "application/pdf" });
// Create a URL for the Blob
const pdfUrl = URL.createObjectURL(pdfBlob);
// Create a hidden download link
const downloadLink = document.createElement("a");
downloadLink.href = pdfUrl;
downloadLink.download = "document.pdf"; // Set file name
// Append, trigger click, and remove link
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
} else {
console.error("Binary file data not found in Weweb collection.");
}