twisted / treq

Python requests like API built on top of Twisted's HTTP client.
Other
587 stars 139 forks source link

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

Open om26er opened 4 years ago

om26er commented 4 years ago

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.

twm commented 4 years ago

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

om26er commented 4 years ago

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

It worked, yay!

glyph commented 4 years ago

The error message could definitely be better here.