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

Failed IBodyProducer coercion produces opaque TypeError on text mode files #286

Open
om26er opened this issue Jul 31, 2020 · 3 comments
Open

Comments

@om26er
Copy link

om26er commented Jul 31, 2020

It seems the API to upload files is broken. Below is the code that I am using to upload a file to Infura's IPFS gateway

from twisted.internet.task import react

import treq


_IPFS_API_GATEWAY = 'https://ipfs.infura.io:5001/api/v0'
_IPFS_ADD_FILE_ENDPOINT = f'{_IPFS_API_GATEWAY}/add'


def _upload_to_infura():
    f = open('file.txt')
    return treq.post(_IPFS_ADD_FILE_ENDPOINT, files={'file': f})


def main(reactor, *args):
    d = _upload_to_infura()
    d.addCallback(lambda response: print(response))
    return d

react(main, [])

On running the code, this is what I see

Traceback (most recent call last):
  File "infura.py", line 20, in <module>
    react(main, [])
  File "/home/om26er/scm/crossbario/xbr-www/venv/lib/python3.8/site-packages/twisted/internet/task.py", line 909, in react
    finished = main(_reactor, *argv)
  File "infura.py", line 16, in main
    d = _upload_to_infura()
  File "infura.py", line 12, in _upload_to_infura
    return treq.post(_IPFS_ADD_FILE_ENDPOINT, files={'file': f})
  File "/home/om26er/scm/crossbario/xbr-www/venv/lib/python3.8/site-packages/treq/api.py", line 33, in post
    return _client(**kwargs).post(url, data=data, **kwargs)
  File "/home/om26er/scm/crossbario/xbr-www/venv/lib/python3.8/site-packages/treq/client.py", line 137, in post
    return self.request('POST', url, data=data, **kwargs)
  File "/home/om26er/scm/crossbario/xbr-www/venv/lib/python3.8/site-packages/treq/client.py", line 201, in request
    files = list(_convert_files(files))
  File "/home/om26er/scm/crossbario/xbr-www/venv/lib/python3.8/site-packages/treq/client.py", line 318, in _convert_files
    yield (param, (file_name, content_type, IBodyProducer(fobj)))
TypeError: ('Could not adapt', <_io.TextIOWrapper name='file.txt' mode='r' encoding='UTF-8'>, <InterfaceClass twisted.web.iweb.IBodyProducer>)

The library chickens out even before sending data on the wire.

@om26er om26er changed the title File Upload is Broken [treq.post()] File Upload is Broken -- treq.post() Jul 31, 2020
@twm
Copy link
Contributor

twm commented Aug 1, 2020

@om26er Have you tried opening the file in binary mode?

@om26er
Copy link
Author

om26er commented Aug 2, 2020

@om26er Have you tried opening the file in binary mode?

It worked, yay!

@glyph
Copy link
Member

glyph commented Aug 4, 2020

The error message could definitely be better here.

@twm twm changed the title File Upload is Broken -- treq.post() Failed IBodyProducer coercion produces opaque TypeError on text mode files Sep 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants