I am developing a http-functions.js function that is supposed to fetch a binary file, then push it to the browser. This needs to be server side for some future security. It appears as though the response stream is corrupt.
import {fetch} from ‘wix-fetch’;
export async function get_sample(request) {
if (request.path[0] === “findMe”) {
const url = “https://www.utoledo.edu/library/help/guides/docs/apastyle.pdf”;
var contentType;
const results = await fetch(url, {method: ‘get’})
.then( function (result) {
contentType = result.headers.get(‘content-type’);
return result.buffer();});
let response = ok({headers: {
“Content-Type”: “application/pdf”,
“Content-Disposition”:“attachment;filename=‘downloaded.pdf’”
},
body:results.toString(‘binary’)});
return response;
}
const body = “Can’t find it!”;
return notFound({body: body});
}
The above code does not properly send the file. The file is corrupt. When I am running this code via a Wix jws function, I can see that the Buffer data appears to have been modified (UTF8 conversion?).
Can anyone see any glaring issues in this code?