1

So I wrote Slack reporter for my automated tests and wanted to switch from deprecated module 'request' to 'https' module. I changed the request sending a normal message but I don't know how to create a request for sending a file. I can't find any example in node documentation (no POST examples for 'https' there) nor any example of that kind of use on the internet. Can anyone help me with this?

That's the working request:

function sendLogFile() {
  console.log("Sending log file to slack...");
  return new Promise((resolve, reject) => {
    request.post(
      {
        url: fileUploadUrl,
        formData: {
          token: token,
          method: "POST",
          title: "Test Log File",
          filename: "testLog.txt",
          filetype: "auto",
          channels: "***",
          file: fs.createReadStream("testLog.txt")
        }
      },
      function(err, response) {
        if (response.body.includes("created"))
          resolve("File send successfully!");
        if (response.body.includes("error")) reject(response.body);
        if (err) reject(err);
      }
    );
  });
}

And this is kinda (SEE THE EDIT BELOW) what I want (but it's not working):

function sendLogFile() {
  return new Promise((resolve, reject) => {
    const requestOptions = {
      url: fileUploadUrl,
      headers: headers,
      formData: {
        token: token,
        method: "POST",
        title: "Test Log File",
        filename: "testLog.txt",
        filetype: "auto",
        channels: "***",
        file: fs.createReadStream("testLog.txt")
      }
    };

    const req = https.request(requestOptions, res => {
      res.on("data", d => resolve(d));
    });
    req.on("error", e => {
      reject(e);
    });

    // Probably that's the part where I'm stuck:
    req.write('????????')

    req.end();
  });
}

I know there is slackapi for node but the thing is I need this reporter to be without any additional packages. And I know it's possible with request.promise or xhr but I need this to be 'https'.

EDIT: Ok, so I was trying to get somewhere and I think it should look more like:

const file = fs.createReadStream("testLog.txt");

const options = {
  channels: "***",
  hostname: "slack.com",
  port: 443,
  path: '/api/files.upload',
  method: 'POST',
  headers: {
    'Authorization': "Bearer ***",
    'Content-Type': 'application/json; charset=utf-8',
  }
}

const req = https.request(options, res => {
  console.log(`statusCode: ${res.statusCode}`)

  res.on('data', d => {
    process.stdout.write(d)
  })
})

req.on('error', error => {
  console.error(error)
})

req.write(data)
req.end()

But I have no idea how to past file to req.write(data) since 'data' has to be string, Buffer, ArrayBuffer, Array, or Array-like Object. So, for now, the response I get is:

statusCode: 200 {"ok":false,"error":"no_file_data"}%

And I'm also not sure if it's possible because slack API says the header should be formData but this response suggests this approach is fine I guess. Anyone, please?

VivaceNonTroppo
  • 456
  • 1
  • 3
  • 13

1 Answers1

0

If you refer to https documentation you can see that options object does not accept such property as formData.

Instead, you should try to send the post data like in this answer.

  • I did create a https.request like in the post you linked and it's working, but this is text-based message. I asked about sending the file which is a different type, with headers { "Content-Type": "multipart/form-data" }. So it won't work. Also, what would you put in req.write() if normally there is stringify JSON with your message? – VivaceNonTroppo Feb 21 '20 at 15:29
  • I changed it so it doesn't containt formData, like this: const requestOptions = { method: "POST", url: fileUploadUrl, headers: headers, token: token, title: "Test Log File", filename: "testLog.txt", filetype: "auto", channels: "GHHAZCKS6", file: fs.createReadStream("testLog.txt") }; And all I got is: {"ok":false,"error":"unknown_method","req_method":"POST"} – VivaceNonTroppo Feb 21 '20 at 15:30