从节点中的本地文件系统上传文件出现 400 错误

发布于 2025-01-11 06:00:45 字数 1134 浏览 0 评论 0原文

我正在尝试使用 multipart/form-data 将文件上传到 API。

我能够使用失眠成功地做到这一点,如下所示:

在此处输入图像描述

标头设置为 Content-Type multipart/form-data 并且还设置了基本身份验证。

当我尝试在代码中复制此内容时,我收到 400 错误。

这是我现在所拥有的:

const URL = 'https://api.app.uploadplace.com/v1/';
const token = 'randomkey';

async function readFile(path: string) {
  return new Promise((resolve, reject) => {
    fs.readFile(path, 'utf-8', function (err, data) {
      if (err) {
        reject(err);
      }

      resolve(data);
    });
  });
}

export const Upload = async (path: string) => {

  const FormData = require('form-data');
  const form = new FormData();

  let file = await readFile(path);

  form.append('file', file);

  const config = {
    headers: {
      'content-type': 'multipart/form-data'
    },
    auth: {
      username: token,
      password: ''
    }
  }

  await axios.post(URL, form, config).then(() => {
    console.log('success')
  }).catch(err => {
    console.log(err);
  })
};

I am trying to upload a file to an API using multipart/form-data.

I am able to successfully do this using insomnia as follows:

enter image description here

Headers are set to Content-Type multipart/form-data and the basic auth is also set.

When I try to replicate this in my code I receive a 400 error.

Here is what I have at the moment:

const URL = 'https://api.app.uploadplace.com/v1/';
const token = 'randomkey';

async function readFile(path: string) {
  return new Promise((resolve, reject) => {
    fs.readFile(path, 'utf-8', function (err, data) {
      if (err) {
        reject(err);
      }

      resolve(data);
    });
  });
}

export const Upload = async (path: string) => {

  const FormData = require('form-data');
  const form = new FormData();

  let file = await readFile(path);

  form.append('file', file);

  const config = {
    headers: {
      'content-type': 'multipart/form-data'
    },
    auth: {
      username: token,
      password: ''
    }
  }

  await axios.post(URL, form, config).then(() => {
    console.log('success')
  }).catch(err => {
    console.log(err);
  })
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小猫一只 2025-01-18 06:00:45

问题是您将文件内容作为字符串附加到表单数据中。

相反,附加文件可读流...

form.append("file", fs.createReadStream(path));

const config = {
  headers: form.getHeaders() // this includes correct mime boundary tokens
  auth: {
    username: token,
    password: ''
  }
}

请参阅 https://github.com/axios/axios #表单数据

The problem is that you are appending the file contents as a string to your form data.

Instead, append the file readable stream...

form.append("file", fs.createReadStream(path));

const config = {
  headers: form.getHeaders() // this includes correct mime boundary tokens
  auth: {
    username: token,
    password: ''
  }
}

See https://github.com/axios/axios#form-data

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文