在OpenShift V3中使用REST api上传图像时出现TypeError

时间:2017-01-16 04:21:40

标签: python django django-rest-framework openshift openshift-nextgen

我有 Django App ,可通过REST-api接受图片。我在本地使用 POSTMAN 测试了api,它与图片上传完美配合。后来我在 OpenShift V3 上部署了它,当我尝试使用POSTMAN进行上传过程时,它说的是

enter image description here

服务器追溯

Environment:


Request Method: POST
Request URL: http://django-psql-persistent-sampledjangoex.44fs.preview.openshiftapps.com/myapp/sample/

Django Version: 1.9
Python Version: 3.5.1
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'debug_toolbar',
 'welcome',
 'rest_framework')
Installed Middleware:
('debug_toolbar.middleware.DebugToolbarMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'whitenoise.middleware.WhiteNoiseMiddleware')



Traceback:

File "/opt/app-root/src/.local/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
  174.                     response = self.process_exception_by_middleware(e, request)

File "/opt/app-root/src/.local/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
  172.                     response = response.render()

File "/opt/app-root/src/.local/lib/python3.5/site-packages/django/template/response.py" in render
  160.             self.content = self.rendered_content

File "/opt/app-root/src/.local/lib/python3.5/site-packages/rest_framework/response.py" in rendered_content
  71.         ret = renderer.render(self.data, media_type, context)

File "/opt/app-root/src/.local/lib/python3.5/site-packages/rest_framework/renderers.py" in render
  104.             separators=separators

File "/opt/rh/rh-python35/root/usr/lib64/python3.5/json/__init__.py" in dumps
  237.         **kw).encode(obj)

File "/opt/rh/rh-python35/root/usr/lib64/python3.5/json/encoder.py" in encode
  199.         chunks = self.iterencode(o, _one_shot=True)

File "/opt/rh/rh-python35/root/usr/lib64/python3.5/json/encoder.py" in iterencode
  257.         return _iterencode(o, 0)

File "/opt/app-root/src/.local/lib/python3.5/site-packages/rest_framework/utils/encoders.py" in default
  64.         return super(JSONEncoder, self).default(obj)

File "/opt/rh/rh-python35/root/usr/lib64/python3.5/json/encoder.py" in default
  180.         raise TypeError(repr(o) + " is not JSON serializable")

Exception Type: TypeError at /myapp/sample/
Exception Value: b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x06\x04\x05\x06\x05\x04\x06\x06\x05\x06\x07\x07\x06\x08\n' is not JSON serializable

以下是source Code of the Program

注意:我可以通过 DRF-Browsable api

进行上传过程

1 个答案:

答案 0 :(得分:1)

首先,我要感谢@ Raz。实际上POSTMAN工具导致了这个问题。我使用Python的Request库测试了OpenShift-v3 api。它工作:)
我用的代码;

import requests
from requests.auth import HTTPBasicAuth

url = 'http://django-psql-persistent-sampledjangoex.44fs.preview.openshiftapps.com/myapp/sample/'
filess = {'image': open('sampleImage.png', 'rb')}
r = requests.post(url,data={'name': 'value1', 'number': 12573474},auth=HTTPBasicAuth('admin', 'admin'),files=filess)