python - Sending file with Flask test client raises "FileStorage is not JSON serializable" -
i testing sending files flask route. however, error typeerror: <filestorage: 'test_photo.jpg' ('image/jpeg')> not json serializable
. looking through route, '/new-ad/write-details
, there nothing attempting json serialize field.
video_file = open(test_helpers.get_dummy_file('test_video.mp4'), 'rb') image_file = open(test_helpers.get_dummy_file('test_photo.jpg'), 'rb') response = self.app.post( '/new-ad/write-details', buffered=true, content_type='multipart/form-data', data={ 'location_id': db.session.query(location).first().id, 'category_id': db.session.query(category).first().id, 'title': 'test title', 'body': 'test body', 'add_video': (video_file, 'test_video.mp4'), 'add_images': (image_file, 'test_photo.jpg')}) assert response.status_code == 302 assert response.location.partition('?')[0] == 'http://localhost/new-ad/preview' ad_id = response.location.partition('?')[2].partition('=')[2] video_file.close() image_file.close()
traceback (most recent call last): file "tests.py", line 144, in test_ads_creation_routes 'add_images': (image_file, 'test_photo.jpg')}) file "/home/dev/projects/ggp/lib/python3.5/site-packages/werkzeug/test.py", line 801, in post return self.open(*args, **kw) file "/home/dev/projects/ggp/lib/python3.5/site-packages/flask/testing.py", line 127, in open follow_redirects=follow_redirects) file "/home/dev/projects/ggp/lib/python3.5/site-packages/werkzeug/test.py", line 764, in open response = self.run_wsgi_app(environ, buffered=buffered) file "/home/dev/projects/ggp/lib/python3.5/site-packages/werkzeug/test.py", line 677, in run_wsgi_app rv = run_wsgi_app(self.application, environ, buffered=buffered) file "/home/dev/projects/ggp/lib/python3.5/site-packages/werkzeug/test.py", line 884, in run_wsgi_app app_rv = app(environ, start_response) file "/home/dev/projects/ggp/lib/python3.5/site-packages/flask/app.py", line 1997, in __call__ return self.wsgi_app(environ, start_response) file "/home/dev/projects/ggp/lib/python3.5/site-packages/flask/app.py", line 1985, in wsgi_app response = self.handle_exception(e) file "/home/dev/projects/ggp/lib/python3.5/site-packages/flask/app.py", line 1540, in handle_exception reraise(exc_type, exc_value, tb) file "/home/dev/projects/ggp/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise raise value file "/home/dev/projects/ggp/lib/python3.5/site-packages/flask/app.py", line 1982, in wsgi_app response = self.full_dispatch_request() file "/home/dev/projects/ggp/lib/python3.5/site-packages/flask/app.py", line 1615, in full_dispatch_request return self.finalize_request(rv) file "/home/dev/projects/ggp/lib/python3.5/site-packages/flask/app.py", line 1632, in finalize_request response = self.process_response(response) file "/home/dev/projects/ggp/lib/python3.5/site-packages/flask/app.py", line 1858, in process_response self.save_session(ctx.session, response) file "/home/dev/projects/ggp/lib/python3.5/site-packages/flask/app.py", line 924, in save_session return self.session_interface.save_session(self, session, response) file "/home/dev/projects/ggp/lib/python3.5/site-packages/flask/sessions.py", line 363, in save_session val = self.get_signing_serializer(app).dumps(dict(session)) file "/home/dev/projects/ggp/lib/python3.5/site-packages/itsdangerous.py", line 565, in dumps payload = want_bytes(self.dump_payload(obj)) file "/home/dev/projects/ggp/lib/python3.5/site-packages/itsdangerous.py", line 847, in dump_payload json = super(urlsafeserializermixin, self).dump_payload(obj) file "/home/dev/projects/ggp/lib/python3.5/site-packages/itsdangerous.py", line 550, in dump_payload return want_bytes(self.serializer.dumps(obj)) file "/home/dev/projects/ggp/lib/python3.5/site-packages/flask/sessions.py", line 85, in dumps return json.dumps(_tag(value), separators=(',', ':')) file "/home/dev/projects/ggp/lib/python3.5/site-packages/flask/json.py", line 123, in dumps rv = _json.dumps(obj, **kwargs) file "/usr/lib/python3.5/json/__init__.py", line 237, in dumps **kw).encode(obj) file "/usr/lib/python3.5/json/encoder.py", line 198, in encode chunks = self.iterencode(o, _one_shot=true) file "/usr/lib/python3.5/json/encoder.py", line 256, in iterencode return _iterencode(o, 0) file "/home/dev/projects/ggp/lib/python3.5/site-packages/flask/json.py", line 80, in default return _json.jsonencoder.default(self, o) file "/usr/lib/python3.5/json/encoder.py", line 179, in default raise typeerror(repr(o) + " not json serializable") typeerror: <filestorage: 'test_photo.jpg' ('image/jpeg')> not json serializable
the code you've shown works, , not related error you've shown. error indicates you're trying add file object session
. bad idea.
the session cookie, not limited in size sent server every request client makes. adding whole file increases network traffic no reason.
if need file in session, or in json response, add reference path on disk, not data itself. when need file make request serve file separately. web servers @ serving files.
return jsonify({'video': '/path/to/video.mp4'})
if, reason, really need add file data json payload (but really, don't), serialize using base64 first. inefficient in space , time, consider last resort only.
from base64 import b64encode return jsonify({'video': b64encode(video.read())})
Comments
Post a Comment