TypeError:FormData 解析错误缺少最终边界

问题描述 投票:0回答:1

最近,我将一个

Next.js
项目从
node
迁移到了
bun
。不幸的是,我在尝试解析表单数据时收到此错误:

14 | const POST = async (request)=>{
15 |     try {
16 |         console.log("request: ", await request.headers);
17 |         const formData = await request.formData();
                                    ^
TypeError: FormData parse error missing final boundary
      at processTicksAndRejections (:61:77)

从一些搜索来看,当

Content-Type
在获取时被错误地覆盖时,似乎会发生此错误。然而,这并没有发生在这里。这是触发
POST
:

的表单组件
  /// ...
  return (
    <form
      action={action}
      method="post"
      className="mt-4"
      onSubmit={async (e) => {
        e.preventDefault();
        setLoading(true);
        setErrors(null);
        const formData = new FormData(e.currentTarget);
        const response = await fetch(action, {
          method: "POST",
          body: formData,
          redirect: "manual",
        });

        if (response.status === 0) {
          // redirected
          // when using `redirect: "manual"`, response status 0 is returned
          return router.refresh();
        }
        setErrors(await response.json());
        setLoading(false);
      }}
    >);

标题还正确显示了

Content-Type

request:  Headers {
  "host": "localhost:3000",
  "accept": "*/*",
  "accept-language": "en-GB,en;q=0.9",
  "accept-encoding": "gzip, deflate",
  "sec-fetch-mode": "cors",
  "content-type": "multipart/form-data; boundary=----WebKitFormBoundary2sBEsUtE1u6eArLT",
  "origin": "http://localhost:3000",
  "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15",
  "referer": "http://localhost:3000/sign-up",
  "content-length": "244",
  "connection": "keep-alive",
  "sec-fetch-dest": "empty",
  "sec-fetch-site": "same-origin",
  "x-forwarded-host": "localhost:3000",
  "x-forwarded-port": "3000",
  "x-forwarded-proto": "http",
}

后端代码也很正常:

// ...
export const POST = async (request: Request) => {
  try {
    console.log('request: ', await request.headers)
    const formData = await request.formData();
    // console.log('request form data: ', JSON.stringify(request));
    const username = formData.get("username");
    const password = formData.get("password");
// ...

感谢您的帮助!

reactjs node.js next.js form-data bun
1个回答
0
投票

好吧,事实证明这是

bun
本身的问题。我将
bun
完全替换为
node
,它解决了问题。查看更多信息https://github.com/oven-sh/bun/issues/2644

© www.soinside.com 2019 - 2024. All rights reserved.