Aiohttp: TypeError: Can not serialize value type: <class 'int'>

Created on 16 Mar 2019  ·  16Comments  ·  Source: aio-libs/aiohttp

Long story short

When using a post request, I want to upload both file and form data. Now I have a problem. When I am working at the same time, there will be an error that the integer cannot be serialized, and I can't find the cause of the problem. Although the integer is replaced by a string, this problem is solved. I suspect that the code in the process of encoding or converting data data is not very robust.

Expected behaviour

Traceback (most recent call last):
  File "/home/py/.virtualenv/youka/lib/python3.7/site-packages/aiohttp/client_reqrep.py", line 482, in update_body_from_data
    body = payload.PAYLOAD_REGISTRY.get(body, disposition=None)
  File "/home/py/.virtualenv/youka/lib/python3.7/site-packages/aiohttp/payload.py", line 106, in get
    raise LookupError()
aiohttp.payload.LookupError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/py/.virtualenv/youka/lib/python3.7/site-packages/aiohttp/formdata.py", line 126, in _gen_form_data
    value, headers=headers, encoding=self._charset)
  File "/home/py/.virtualenv/youka/lib/python3.7/site-packages/aiohttp/payload.py", line 63, in get_payload
    return PAYLOAD_REGISTRY.get(data, *args, **kwargs)
  File "/home/py/.virtualenv/youka/lib/python3.7/site-packages/aiohttp/payload.py", line 106, in get
    raise LookupError()
aiohttp.payload.LookupError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/py/project/chd_youka/async_spider/chaojiying.py", line 65, in <module>
    loop.run_until_complete(main())                 #1902 验证码类型  官方网站>>价格体系 3.4+版 print 后要加()
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 568, in run_until_complete
    return future.result()
  File "/home/py/project/chd_youka/async_spider/chaojiying.py", line 54, in main
    code = await chaojiying.PostPic(im, 4004,session)
  File "/home/py/project/chd_youka/async_spider/chaojiying.py", line 38, in PostPic
    async with session.post(url='http://upload.chaojiying.net/Upload/Processing.php', data=params, headers=self.headers) as response:
  File "/home/py/.virtualenv/youka/lib/python3.7/site-packages/aiohttp/client.py", line 1005, in __aenter__
    self._resp = await self._coro
  File "/home/py/.virtualenv/youka/lib/python3.7/site-packages/aiohttp/client.py", line 466, in _request
    ssl=ssl, proxy_headers=proxy_headers, traces=traces)
  File "/home/py/.virtualenv/youka/lib/python3.7/site-packages/aiohttp/client_reqrep.py", line 294, in __init__
    self.update_body_from_data(data)
  File "/home/py/.virtualenv/youka/lib/python3.7/site-packages/aiohttp/client_reqrep.py", line 484, in update_body_from_data
    body = FormData(body)()
  File "/home/py/.virtualenv/youka/lib/python3.7/site-packages/aiohttp/formdata.py", line 148, in __call__
    return self._gen_form_data()
  File "/home/py/.virtualenv/youka/lib/python3.7/site-packages/aiohttp/formdata.py", line 131, in _gen_form_data
    type(value), headers, value)) from exc
TypeError: Can not serialize value type: <class 'int'>
 headers: {}
 value: 4004

Actual behaviour

When the form data contains both file objects (perhaps binary objects) and integers

    async def PostPic(self, im, codetype, session):
        """
        im: Picture byte
        codetype: http://www.chaojiying.com/price.html
        """
        params = {
            'codetype': codetype,
        }
        params.update(self.base_params)
        files = {'userfile':im}
        params.update(files)
        print(params)
        async with session.post(url='http://upload.chaojiying.net/Upload/Processing.php', data=params, headers=self.headers) as response:
            return await response.text()

image

Steps to reproduce

Your environment