python-redmine
python-redmine copied to clipboard
BytesIO is not JSON serializable
The uploads portion of the following example code fails with the error below for create(), removing the uploads assignment makes the call successful:
https://python-redmine.com/resources/issue.html?highlight=BytesIO
issue = redmine.issue.create( project_id='vacation', subject='Vacation', tracker_id=8, description='foo', status_id=3, priority_id=7, assigned_to_id=123, watcher_user_ids=[123], parent_issue_id=345, start_date=datetime.date(2014, 1, 1), due_date=datetime.date(2014, 2, 1), estimated_hours=4, done_ratio=40, custom_fields=[{'id': 1, 'value': 'foo'}, {'id': 2, 'value': 'bar'}], uploads=[{'path': '/absolute/path/to/file'}, {'path': BytesIO(b'I am content of file 2')}] )
Error:
File "/home/dev/python-zulip-api-SE/zulip-api-py3-venv/lib/python3.10/site-packages/redminelib/managers/base.py", line 187, in create
response = self.redmine.engine.request(self.resource_class.http_method_create, url, data=request)
File "/home/dev/python-zulip-api-SE/zulip-api-py3-venv/lib/python3.10/site-packages/redminelib/engines/base.py", line 82, in request
kwargs = self.construct_request_kwargs(method, headers, params, data)
File "/home/dev/python-zulip-api-SE/zulip-api-py3-venv/lib/python3.10/site-packages/redminelib/engines/base.py", line 66, in construct_request_kwargs
kwargs['data'] = json.dumps(data)
File "/usr/lib/python3.10/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python3.10/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.10/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.10/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type BytesIO is not JSON serializable
Redmine Server: 5.0.2 Python: 3.1 OS: Windows 11 Pro
Hi @pagogc ,
Can't reproduce it here, works fine for me, can you show your actual code and not the copy'n'paste from the docs, because the only way this error can happen is if you mistype path
key in uploads
argument to something else like paths or whatever.
saw same error when used args dictionary as update method parameter, like this: redmine.issue.update(issue_id, **argsdict)). My mistake was in adding 'uploads' arg to dictionary:
fileobj = {'path': BytesIO(fh.read()), 'content_type': content_type, 'filename': args.filepath[i].split(os.sep)[-1]}
wrong way: argsdict.update(fileobj) right way: argsdict.update({'uploads': [fileobj]})
@obviliontsk That's a good hint, thanks.
Basically a rule of thumb here is to put everything in a dict with uploads
being the key and your data being the value.
Closing this due to inactivity.