Basavaraj Lamani
Basavaraj Lamani

Reputation: 255

Not able to upload file using slack api files.upload

This question may seem duplicate but I have tried a lot but did not get success. I am trying to upload html file using https://slack.com/api/files.upload API but I am getting below error always. response {'ok': False, 'error': 'no_file_data'}

I went through documentation [a link]https://api.slack.com/methods/files.upload and tried with different options but still i am getting the same response {'ok': False, 'error': 'no_file_data'}

Also i have seen many similar questions in stack overflow but none of them resolved the problem. [a link]no_file_data error when using Slack API upload [a link]How to upload files to slack using file.upload and requests

Below is my code.

import requests

def post_reports_to_slack(html_report):
    """
    """
    url = "https://slack.com/api/files.upload"

    # my_file = {html_report, open(html_report, 'rb'), 'html'}

    data = {
        "token": bot_user_token,
        "channels": channel_name,
        "file": html_report,
        "filetype": "html"
    }

    # data = "token=" + bot_user_token + \
    #        "&channels=" + channel_name +\
    #        "&file=" + html_report + "&filetype=" + "html"

    response = requests.post(
        url=url, data=data,
        headers={"Content-Type": "application/x-www-form-urlencoded"})
    print("response", response)
    print(response.json())
    if response.status_code == 200:
        print("successfully completed post_reports_to_slack "
                      "and status code %s" % response.status_code)
    else:
        print("Failed to post report on slack channel "
                      "and status code %s" % response.status_code)

Please help to resolve the issue.

Upvotes: 1

Views: 6425

Answers (2)

Thanh Lai
Thanh Lai

Reputation: 1

I use argument "content" instead of using "file". The error "no_file_data" disappear.

public class SlackFileUploadBody {
  @FormParam("content")
  @PartType(MediaType.APPLICATION_OCTET_STREAM)
  public InputStream content;

  @FormParam("channels")
  @PartType(MediaType.TEXT_PLAIN)
  public String channels;

  @FormParam("initial_comment")
  @PartType(MediaType.TEXT_PLAIN)
  public String initialComment;

  @FormParam("title")
  @PartType(MediaType.TEXT_PLAIN)
  public String title;
}

@RegisterRestClient(configKey = "slack-api")
@RegisterClientHeaders(SlackHeaderFactory.class)
public interface ISlackService {
    @POST
    @Path("/files.upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.TEXT_PLAIN)
    String uploadFile(@MultipartForm SlackFileUploadBody body);
}

Upvotes: 0

Basavaraj Lamani
Basavaraj Lamani

Reputation: 255

I was needed to add "content" argument and "filename" argument instead of "file" argument in files.upload API payload, Now file uploading to slack channel is working fine.

import requests

def post_reports_to_slack(html_report):
    url = "https://slack.com/api/files.upload"

    with open(html_report) as fh:
        html_data = fh.read()

    data = {
        "token": bot_user_token,
        "channels": "#channel_name",
        "content": html_data,
        "filename": "report.html",
        "filetype": "html",
    }

    response = requests.post(
        url=url, data=data,
        headers={"Content-Type": "application/x-www-form-urlencoded"})
    if response.status_code == 200:
        print("successfully completed post_reports_to_slack "
                      "and status code %s" % response.status_code)
    else:
        print("Failed to post report on slack channel "
                      "and status code %s" % response.status_code)

Upvotes: 8

Related Questions