Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

files_upload failing to upload custom FormData object #654

Closed
5 of 9 tasks
GerhardOfRivia opened this issue Apr 20, 2020 · 2 comments
Closed
5 of 9 tasks

files_upload failing to upload custom FormData object #654

GerhardOfRivia opened this issue Apr 20, 2020 · 2 comments
Labels
question M-T: User needs support to use the project Version: 2x web-client

Comments

@GerhardOfRivia
Copy link

Description

Describe your issue here.

What type of issue is this? (place an x in one of the [ ])

  • bug
  • enhancement (feature request)
  • question
  • documentation related
  • testing related
  • discussion

Requirements (place an x in each of the [ ])

  • I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I've searched for any related issues and avoided creating a duplicate issue.

Bug Report

I am trying to upload a in memory string to slack as a file. Probably something inside of using an io.BytesIO object.

Reproducible in:

slackclient version: 2.5.0

python version: 3.6.10

OS version(s): Docker Alpine

$ cat /etc/alpine-release 
3.11.5

Steps to reproduce:

        try:
            buff = io.BytesIO(b'here is my data but not sure what is wrong.......')
            buff.seek(0)
            data = FormData()
            data.add_field('file', buff, filename='output.txt', content_type='text')
            client = slack.WebClient(token=os.environ['SLACK_API_TOKEN'])
            response = client.files_upload(channels='#my-random-channel', file=data)
            logger.info('html slack post complete %s', response)
        except Exception as err:
            logger.exception(err)

Expected result:

What you expected to happen, the file to be uploaded?

Actual result:

     response = client.files_upload(channels='#my-random-channel', file=data)
   File "/usr/local/lib/python3.6/site-packages/slack/web/client.py", line 970, in files_upload
     return self.api_call("files.upload", files={"file": file}, data=kwargs)
   File "/usr/local/lib/python3.6/site-packages/slack/web/base_client.py", line 171, in api_call
     return self._event_loop.run_until_complete(future)
   File "/usr/local/lib/python3.6/asyncio/base_events.py", line 488, in run_until_complete
     return future.result()
   File "/usr/local/lib/python3.6/site-packages/slack/web/base_client.py", line 226, in _send
     return SlackResponse(**{**data, **res}).validate()
   File "/usr/local/lib/python3.6/site-packages/slack/web/slack_response.py", line 176, in validate
     raise e.SlackApiError(message=msg, response=self)
 slack.errors.SlackApiError: The request to the Slack API failed.
 The server responded with: {'ok': False, 'error': 'no_file_data'}

Attachments:

Logs, screenshots, screencast, sample project, funny gif, etc.

@seratch seratch added question M-T: User needs support to use the project Version: 2x web-client labels Apr 21, 2020
@seratch
Copy link
Member

seratch commented Apr 21, 2020

Hello @AsynchronousGillz 👋

Do you have some reason to directly use aiohttp.FormData to construct the form data? If not, just passing io.BytesIO data as file parameter works for you. Here is a minimum working example.

import io
import logging
import os

logger = logging.getLogger(__name__)
bot_token = os.environ["SLACK_BOT_TOKEN"]
client: WebClient = WebClient(token=bot_token)
channel_ids = self.sync_client, self.logger, ",".join(["C123456789"])
buff = io.BytesIO(b'here is my data but not sure what is wrong.......')
buff.seek(0)
upload = client.files_upload(
    file=buff,
    filename="output.text",
    filetype="text",
    channels=channel_ids,
)
logger.debug("File uploaded - %s", upload)

If you need to use aiohttp.FormData for some reason, unfortunately, the WebClient doesn't accept the value and we're not planning to support the type.

seratch added a commit to seratch/python-slack-sdk that referenced this issue Apr 21, 2020
Now it's possible run
`python setup.py run_all_tests --integration-test-target=web/test_issue_654.py`
@GerhardOfRivia
Copy link
Author

GerhardOfRivia commented Apr 21, 2020

I did not think I could just use the BytesIO object like that..... That worked perfectly Thank You!

seratch added a commit to seratch/python-slack-sdk that referenced this issue Apr 21, 2020
Now it's possible run
`python setup.py run_all_tests --integration-test-target=web/test_issue_654.py`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question M-T: User needs support to use the project Version: 2x web-client
Projects
None yet
Development

No branches or pull requests

2 participants