1

I am using a form to collect information including a file and I am trying to upload that file to a slack channel on submission. I have tried using the suggestions in this answer: https://stackoverflow.com/a/47597472/8429996, but still getting same error response, 'invalid_form_data'. I successfully sent a message using postMessage method but I want to attach a file from the user's system and upload directly to slack using files.upload. Is this possible?

Here is the code:

upload(e) {
  this.file = e.target.files[0];

  const httpOptions = {
    headers: new HttpHeaders({
      'Content-Type': 'multipart/form-data'
    })
  };

  var formData = new FormData();
  formData.append('token', SLACK_TOKEN);
  formData.append('channels', SLACK_CHANNELS);
  formData.append('file', this.file);


  this.http.post<any>('https://slack.com/api/files.upload', formData, httpOptions).subscribe(
    resp => { console.log(resp) },
    error => { console.log(error) }
  );
}

And the test file object looks like:

File: {
  lastModified: 1606970395000
  lastModifiedDate: Wed Dec 02 2020 23:39:55 GMT-0500 (Eastern Standard Time) {}
  name: "IMG_0320.HEIC"
  size: 1206160
  type: "image/heic"
  webkitRelativePath: ""
}

1 Answers1

1

Figured it out... just removed the headers and it worked.