本文介绍了如何在Windows上使用node.js将图像上传到Slack?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用node.js和请求包通过Slack上传图像,但运气不佳.我从API收到invalid_array_argno_file_data错误.

I'm trying to upload an image via Slack using node.js and the request package, but not having much luck. Either I receive invalid_array_arg or no_file_data errors from the API.

这是我的要求:

    var options = { method: 'POST',
      url: 'https://slack.com/api/files.upload',
      headers: 
       { 'cache-control': 'no-cache',
         'content-type': 'application/x-www-form-urlencoded' },
      form: 
       { token: SLACK_TOKEN,
         channels: SLACK_CHANNEL,
         file: fs.createReadStream(filepath)
        } };

    request(options, function (error, response, body) {
      if (error) throw new Error(error);

      console.log(body);
    });

我看了一些相关的帖子:

I had a look at a few relevant posts:

  • Can I upload an image as attachment with Slack API?
  • Slack API (files.upload) using NodeJS
  • fix files.upload from Buffer with formData options #307

唯一有效的方法是直接使用curl命令,但使用cygwin(CommandPrompt失败:curl: (1) Protocol https not supported or disabled in libcurl).从节点(使用child_process)调用curl的问题,但是在命令提示符中无提示地失败,并且仍然使用cygwin返回no_file_data(将绝对路径传递到文件):

The only thing that worked was using the curl command directly, but using cygwin (CommandPrompt failed: curl: (1) Protocol https not supported or disabled in libcurl). The issue calling curl from node (using child_process) but that silently fails in Command Prompt and still returns no_file_data using cygwin (passing an absolute path to the file):

stdout: {"ok":false,"error":"no_file_data"}
stderr:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   469  100    35  100   434    359   4461 --:--:-- --:--:-- --:--:--  6112

我正在Windows上使用节点v6.9.1.

I'm using node v6.9.1 on Windows.

我想念什么?如何在Windows上通过node.js将图像上传到Slack?

What am I missing ? How can I upload an image to slack via node.js on Windows ?

推荐答案

Slack API错误invalid_array_arg表示传递给Slack的参数格式存在问题. (请参见此处)

The Slack API error invalid_array_arg means that there is a problem with the format of the arguments passed to Slack. (see here)

当对files.upload使用file属性时,Slack会将数据作为multipart/form-data而不是作为application/x-www-form-urlencoded除外.因此,您需要在请求对象中使用formData而不是form.我还删除了标题中不正确的部分.

When using the file property for files.upload, Slack excepts the data as multipart/form-data, not as application/x-www-form-urlencoded. So instead of form, you need to use formData in your request object. I also removed the incorrect part in the header.

这有效:

      var fs = require('fs');
      var request = require('request');

      var SLACK_TOKEN = "xoxp-xxx";
      var SLACK_CHANNEL = "general";
      var filepath = "file.txt";

      var options = { method: 'POST',
      url: 'https://slack.com/api/files.upload',
      headers: 
       { 'cache-control': 'no-cache' },
      formData: 
       { token: SLACK_TOKEN,
         channels: SLACK_CHANNEL,
         file: fs.createReadStream(filepath)
        } };

    request(options, function (error, response, body) {
      if (error) throw new Error(error);

      console.log(body);
    });

这篇关于如何在Windows上使用node.js将图像上传到Slack?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 23:36