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

When using fetch for FormData upload files, lack of"\r\n" #3624

Closed
JoeLeaf opened this issue Sep 19, 2024 · 4 comments · Fixed by #3625
Closed

When using fetch for FormData upload files, lack of"\r\n" #3624

JoeLeaf opened this issue Sep 19, 2024 · 4 comments · Fixed by #3625
Labels

Comments

@JoeLeaf
Copy link

JoeLeaf commented Sep 19, 2024

Version

v20.14.0

Platform

Microsoft Windows NT 10.0.19045.0 x64

Subsystem

No response

What steps will reproduce the bug?

(async () => {
  process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
  const axios = require("axios");
  const pic =
    "https://c2cpicdw.qpic.cn/download?appid=1407&fileid=Cgk3MDcxMDk4MzESFIdXk-Su5zb_v3YXR9IGh5-lYY7lGJmyUyD_CijP6aXtrb-IA1CAvaMB&rkey=CAQSMBcDFfD6aMnZO59Yn6VJT6zMyrgym718ZCG4J280hrOxtR2t-TLMZ5bE21i4-U0fOw&spec=0";
  const url = "https://web.qun.qq.com/cgi-bin/announce/upload_img";
  const cookies =
    "skey=MjHlWn1sIm; pskey=0MtTUBRnoPPWn4dKkwAN8g7jWjv7yz*yYdCOD*qNsCo_; uin=o3220300337";
  let data = new FormData();
  const picBlob = await (await fetch(pic)).blob();
  data.append("bkn", "1733032207");
  data.append("m", "0");
  data.append("source", "troopNotice");
  data.append("qid", "0");
  data.append("pic_up", picBlob, "a.jpg");
  console.log("-------------fetch-------------");
  let res = await fetch(url, {
    headers: {
      cookie: cookies,
    },
    method: "POST",
    body: data,
  });
  console.log(await res.json());
  console.log("-------------axios-------------");
  res = await axios.request({
    method: "post",
    url: "https://web.qun.qq.com/cgi-bin/announce/upload_img",
    headers: {
      cookie: cookies,
    },
    data: data,
  });
  console.log(res.data);
})();

The cookie here may have expired

Using axios can smoothly upload pictures, using fetch will prompt the picture error,By grabbing the package, I found that the loss at the end of the fetch was lost at the end of the upload"\r\n"

Missing "0d 0a 0d 0a" =>"\r\n\r\n"

fetch
axios

How often does it reproduce? Is there a required condition?

Every time

What is the expected behavior? Why is that the expected behavior?

axios

Tail needs“0d0a”

What do you see instead?

fetch

Tail needs“0d0a”

Additional information

No response

@KhafraDev
Copy link
Member

A multipart/form-data body is defined as

A multipart/form-data body contains a series of parts separated by a boundary.

After a cursory glance in the fetch and xhr specs, along with rfc7578, I couldn't find anywhere that mentioned the serialization process requiring a body end with \r\n.

@KhafraDev
Copy link
Member

@nodejs/web-standards if anyone else wants to take a look.

@JoeLeaf
Copy link
Author

JoeLeaf commented Sep 19, 2024

A multipart/form-data body is defined as

A multipart/form-data body contains a series of parts separated by a boundary.

After a cursory glance in the fetch and xhr specs, along with rfc7578, I couldn't find anywhere that mentioned the serialization process requiring a body end with \r\n.

Found a piece of information on this。
At present, I find that there is a lack of "CRLF". I don't know if it is necessary, so I can only use "axios" to complete the code first. Also thank you for reading my questions.🌹

@KhafraDev
Copy link
Member

axios adds two \r\n at the end of the footer, which could be legacy behavior or trying to bypass issues like these. https://github.com/axios/axios/blob/1472163d373dda71f74e86365ee2f298d922f5db/lib/helpers/formDataToStream.js#L78

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants