1
votes

I get this following data through a POST request in Django restframework. I need to serialize this data and this data contains data for multiple models.

data={'admin-1':
    {'first_name':'john'
    ,'last_name':'white'
    ,'job_title':'CEO'
    ,'email':'[email protected]'
    },
'admin-2':
    {'first_name':'lisa'
    ,'last_name':'markel'
    ,'job_title':'CEO'
    ,'email':'[email protected]'
    },
'company-detail':{'description':'We are a renowned engineering company'
,'size':'1-10'
,'industry':'Engineering'
,'url':'http://try.com'
,'logo':''
,'addr1':'1280 wick ter'
,'addr2':'1600'
,'city':'rkville'
,'state':'md'
,'zip_cd':'12000'
,'phone_number_1':'408-393-254'
,'phone_number_2':'408-393-221'}

r = requests.post('http://127.0.0.1:8000/api/create-company-profile/',data=data)
print r.status_code
print r.text

Here is the CreateAPI view -

class CompanyCreateApiView(CreateAPIView):

    def post(self, request, *args, **kwargs):
        print 'request ==', request
        print 'request.data == ', request.data['admin-2']

        import json
        print json.loads(request.data)

        data=json.dumps({'status':'success'})
        return Response(data, status=status.HTTP_200_OK)

I basically need to de-serialize the data but get this error.

request == request.data == job_title Internal Server Error: /api/create-company-profile/ Traceback (most recent call last): File "/Users/prem/.virtualenvs/ghost/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Users/prem/.virtualenvs/ghost/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 57, in wrapped_view return view_func(*args, **kwargs) File "/Users/prem/.virtualenvs/ghost/lib/python2.7/site-packages/django/views/generic/base.py", line 69, in view return self.dispatch(request, *args, **kwargs) File "/Users/prem/.virtualenvs/ghost/lib/python2.7/site-packages/rest_framework/views.py", line 452, in dispatch response = self.handle_exception(exc) File "/Users/prem/.virtualenvs/ghost/lib/python2.7/site-packages/rest_framework/views.py", line 449, in dispatch response = handler(request, *args, **kwargs) File "/Users/prem/Documents/Ghost/positionmatch-new/menkes-server-master/menkesserver/human_resources/views.py", line 81, in post print json.loads(request.data) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/init.py", line 338, in loads return _default_decoder.decode(s) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) TypeError: expected string or buffer

1

1 Answers

0
votes

There should be no need to manually decode the POST data in your view. As long as you are using the JSONParser, as described in the parsers documentation, request.data should already be fully parsed for you.

Additionally, it looks like the request you're sending to the view is probably not acting how you think. If you want to send JSON data with requests, you need to be a little more explicit. As shown in the example in the requests documentation:

>>> import json
>>> url = 'https://api.github.com/some/endpoint'
>>> payload = {'some': 'data'}

>>> r = requests.post(url, data=json.dumps(payload))