Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

javascript - 为什么在Slack Web API上收到无效表单数据错误?(Why am I getting Invalid Form data error on slack web api?)

I'm trying to upload a file to slack but constantly getting an error saying invalid_form_data .

(我正在尝试将文件上传到Slack,但不断收到一条错误消息,说invalid_form_data 。)

I'm building an electronJS app and using the following code to upload the file:

(我正在构建一个electronicJS应用程序,并使用以下代码上传文件:)

const filee = fs.createReadStream(file);
  (async () => {

    const result = await web.files.upload({
      channels: '#general',
      file: filee
    })

  })()

I have even tried to post using axios but ran into the same problem.

(我什至尝试过使用axios发布,??但遇到了同样的问题。)

The code is:

(代码是:)

const upload_url = 'https://slack.com/api/files.upload'
  let file = null
  fs.readFile(file_path, 'utf-8', function (err, data) {
    file = data
  });
  const post = {
    channels: "#general",
    file: file
  }

  axios.post(upload_url, post, {
    headers: {
      'Content-Type': 'multipart/form-data',
      "Authorization": `Bearer ${token}`
    }
  }).then(function (response) {
    console.log(response['data']['ok'])
  }).then(function (err) {
    console.log(err)
  })

I have also tried formData module, like so:

(我也尝试过formData模块,如下所示:)

const upload_url = 'https://slack.com/api/files.upload'
  const formData = new FormData();
  let file = null
  fs.readFile(file_path, function (err, data) {
    file = data
  });

  formData.append("file", file);
  axios.post(upload_url, formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
      "Authorization": `Bearer ${token}`
    }
  }).then(function (response) {
    console.log(response['data']['error'])
  }).then(function (err) {
    console.log(err)
  })

What am I doing wrong?

(我究竟做错了什么?)

  ask by rootkit translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...