0

I have a Django App that accept images through REST-api. I tested the api using POSTMAN locally, it's working perfect with image uploads. Later I deployed it on OpenShift V3 and when I tried the same for uploading process using POSTMAN , it's saying

enter image description here

Server Traceback

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

Here is the source Code of the Program

Note: I can do uploading process through DRF-Browsable api

  • Screenshot is useless. Please provide server traceback. – Raz Jan 16 '17 at 09:00
  • can you provide your code ? – ᴄʀᴏᴢᴇᴛ Jan 16 '17 at 09:36
  • @Raz I updated the question with Traceback, Please check it out –  Jan 16 '17 at 12:43
  • @C̲̅R̲̅O̲̅Z̲̅E̲̅T̲̅ you can find the source code [here](https://drive.google.com/file/d/0B0DlE-Qi4Fu6cEVXUVpxMFZYeFk/view) –  Jan 16 '17 at 12:45
  • Looks like you have exception during the handling of another exception. Try debug this line: `response = self.process_exception_by_middleware(e, request)` in /opt/app-root/src/.local/lib/python3.5/site-packages/django/core/handlers/base.py. Get value of `e`. This is original exception. Django tried to render it and failed. – Raz Jan 16 '17 at 13:57
  • @Raz I have no problem/error when I run the project locally. Then how can I debug ? –  Jan 16 '17 at 14:03
  • I never used OpenShift. Do you have ssh access to the server? If you have you can just call print next to the line 174 in django/core/handlers/base.py. Additionally try to make request from python with requests library. Not with the postman. I think you have issue in your request. – Raz Jan 16 '17 at 14:10

1 Answers1

1

First of all I would say thanks to @Raz. Actually the tool POSTMAN caused the issue. I tested the OpenShift-v3 api using Python's Request library. It's worked :)
Code I used;

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)
Community
  • 1
  • 1