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

.set method with File from FormData differs in behavior from Node.js (file name is lost) #12325

Closed
kravetsone opened this issue Jul 3, 2024 · 1 comment · Fixed by #12379
Closed
Labels
bug Something isn't working

Comments

@kravetsone
Copy link

What version of Bun is running?

1.1.17+bb66bba1b

What platform is your computer?

Microsoft Windows NT 10.0.22631.0 x64

What steps can reproduce the bug?

I came across this when I was uploading files to Telegram using GramIO. I am sure that this is reproduced in other cases.

The fact is that when I fill in the FormData using the .set method, the filename is lost when uploading to Telegram, although in Node.js everything works as expected...

async function sendDocument(body) {
    const response = await fetch(
        `https://api.telegram.org/bot${process.env.BOT_TOKEN}/sendDocument`,
        {
            method: "POST",
            body: body
        }
    );
    
    const json = await response.json();
    
    console.log(json);
}

const formDataSet = new FormData();

formDataSet.set("chat_id", "617580375");
formDataSet.set("document", new File(["some text notes"], "text-notes.txt"));

await sendDocument(formDataSet)

const formDataAppend = new FormData();

formDataAppend.append("chat_id", "617580375");
formDataAppend.append("document", new File(["some text notes"], "text-notes.txt"));

await sendDocument(formDataAppend)

If you try to run this option through Node.js, both options will send a file with the specified name, but if you run it through Bun, then there will be only second file with the correct name (where it was FormData.append used)

image

Verbose logs where .set used (wrong filename)

[fetch] > HTTP/1.1 POST https://api.telegram.org/bot[REDACTED]/sendDocument
[fetch] > Content-Type: multipart/form-data; boundary="-WebkitFormBoundary980f0dfe1eed40e094766072fdf6ca05"
[fetch] > Connection: keep-alive
[fetch] > User-Agent: Bun/1.1.17
[fetch] > Accept: */*
[fetch] > Host: api.telegram.org
[fetch] > Accept-Encoding: gzip, deflate, br
[fetch] > Content-Length: 349

Verbose logs where .append used

[fetch] > HTTP/1.1 POST https://api.telegram.org/bot[REDACTED]/sendDocument
[fetch] > Content-Type: multipart/form-data; boundary="-WebkitFormBoundary233a1c42edc94d80ba8d2c57b543727c"
[fetch] > Connection: keep-alive
[fetch] > User-Agent: Bun/1.1.17
[fetch] > Accept: */*
[fetch] > Host: api.telegram.org
[fetch] > Accept-Encoding: gzip, deflate, br
[fetch] > Content-Length: 363

as we can see Content-Length differs

Because of this, file uploads to GramIO didn't work correctly for a long time, but I'm glad to get to the truth!

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

@billywhizz
Copy link
Contributor

PR here #12379

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants