Open joulev opened 10 months ago
For more information, the following code snippet (thanks to @/RiskyMH), with a rather manually dirty way of submitting multipart/form-data
, works
const form = ...;
const textBody = await new Response(form).text();
const pdfResponse = await fetch("https://texlive.net/cgi-bin/latexcgi", {
method: "POST",
body: textBody,
headers: {
"Content-Type": `multipart/form-data; boundary=${textBody
.split("\n")[0]
.slice(2)}`,
},
});
const buffer = await pdfResponse.arrayBuffer();
console.log(buffer);
For more information, the following code snippet (thanks to @/RiskyMH), with a rather manually dirty way of submitting
multipart/form-data
, worksconst form = ...; const textBody = await new Response(form).text(); const pdfResponse = await fetch("https://texlive.net/cgi-bin/latexcgi", { method: "POST", body: textBody, headers: { "Content-Type": `multipart/form-data; boundary=${textBody .split("\n")[0] .slice(2)}`, }, }); const buffer = await pdfResponse.arrayBuffer(); console.log(buffer);
This method seems to only be applicable for text transmission, not for transmitting images or other binary data. I encountered this problem as well (boundary="xxxx", with extra double quotes causing the destination server to fail in parsing it correctly).
Same error here😿
For more information, the following code snippet (thanks to @/RiskyMH), with a rather manually dirty way of submitting
multipart/form-data
, worksconst form = ...; const textBody = await new Response(form).text(); const pdfResponse = await fetch("https://texlive.net/cgi-bin/latexcgi", { method: "POST", body: textBody, headers: { "Content-Type": `multipart/form-data; boundary=${textBody .split("\n")[0] .slice(2)}`, }, }); const buffer = await pdfResponse.arrayBuffer(); console.log(buffer);
This method seems to only be applicable for text transmission, not for transmitting images or other binary data. I encountered this problem as well (boundary="xxxx", with extra double quotes causing the destination server to fail in parsing it correctly).
My solution for sending (uploading) pdf was this:
const formData = new FormData();
formData.append('fileupload', Bun.file(`files/${name}`), name);
const blob = await new Response(formData).blob();
const response = await fetch("https://someurl.com/upload/pdf", {
method: "POST",
body: await blob.arrayBuffer(),
headers: {
"Content-Type": blob.type
},
});
As far as I can tell, Bun doesn't come close to handling a post body of type FormData correctly. Kind of a big miss, and I'm disappointed it is approaching a year now and nothing has been done.
What version of Bun is running?
1.0.20+09d51486e
What platform is your computer?
Darwin 23.2.0 arm64 arm
What steps can reproduce the bug?
Run this JavaScript file
What is the expected behavior?
If you run the file above with
node
, it gets the correct data (which is a PDF file):What do you see instead?
But when you run it with
bun
, it gets this textual responseAdditional information
This particular URL response is a 301 Moved Permanently, then when you follow the redirect you get the URL of the PDF file that you download to the big
ArrayBuffer
above. Maybe it is at this point that Bun'sfetch
acts differently from Node's?