No JSON object could be decoded - Django request.body
<p>I am making web service for posting comments from smart phone, Below is my code </p>

<pre><code>@api_view(['POST'])
def comment_post(request,newsId=None):
    data = json.loads(request.body)
    responseData= dict({
       "result": list()
       })
    if(newsId):
        commentNews  = models.Comments.objects.create()
        commentNews.comment_description = data.get('comment_description').strip()
        commentNews.like_count = int(data.get('like_count'))
        commentNews.user_name = data.get('user_name').strip()
        commentNews.user_email_id = data.get('user_email_id').strip()
        commentNews.parent_comment = data.get('parent_comment').strip()
        commentNews.save()
        subscribed_user = models.SubscribedUsers.objects.create(username=data.get('user_name').strip(),email=data.get('user_email_id').strip())
        news = models.News.objects.get(id=int(newsId))
        news.comments.add(commentNews)
        data ={
         'status':'success'
         }
    else:
        data ={
        'status':'failure'
        }
    responseData['result'].append(data)
    return Response(responseData,status=status.HTTP_200_OK) 
</code></pre>

<p>Whenever i check it on local it works, but on server side it gives me below error</p>

<pre><code>ValueError at /service/comment_post/369

No JSON object could be decoded

Request Method:     POST
Request URL:    http://dev.newskhabari.com/service/comment_post/369
Django Version:     1.9.5
Exception Type:     ValueError
Exception Value:    

No JSON object could be decoded

Exception Location:     /usr/local/lib/python2.7/json/decoder.py in raw_decode, line 383
Python Executable:  /var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/bin/python
Python Version:     2.7.6
Python Path:    

['/var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/lib/python2.7/site-packages',
 '/var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/lib/python2.7/site-packages/django',
 '/var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/bin',
 '/var/www/vhosts/newskhabari.com/newskhabari_dev',
 '/usr/local/rvm/gems/ruby-2.2.2/gems/passenger-5.0.30/src/helper-scripts',
 '/var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/lib/python27.zip',
 '/var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/lib/python2.7',
 '/var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/lib/python2.7/plat-linux2',
 '/var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/lib/python2.7/lib-tk',
 '/var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/lib/python2.7/lib-old',
 '/var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7',
 '/usr/local/lib/python2.7/plat-linux2',
 '/usr/local/lib/python2.7/lib-tk',
 '/var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/lib/python2.7/site-packages',
 '/var/www/vhosts/newskhabari.com/newskhabari_dev',
 '/var/www/vhosts/newskhabari.com/newskhabari_dev/app']

Server time:    Mon, 17 Oct 2016 11:35:36 +0530
</code></pre>

<p>I am unable to figure out why it give 
Exception Value: No JSON object could be decoded</p>

<p>I guess, You are using <code>django-rest-framework</code>. So, You don't have to do <code>json.loads()</code>, becasue <code>django-rest-framework</code> provides <code>request.data</code> for <code>POST</code> requests and <code>request.query_params</code> for <code>GET</code> requests, <strong>already parsed in json format</strong>.</p>

<p>So I think this should work for you. </p>

<pre><code>@api_view(['POST'])
def comment_post(request,newsId=None):
responseData= dict({
   "result": list()
   })
if(newsId):
    commentNews  = models.Comments.objects.create()
    commentNews.comment_description = request.data.get('comment_description').strip()
    commentNews.like_count = int(request.data.get('like_count'))
    commentNews.user_name = request.data.get('user_name').strip()
    commentNews.user_email_id = request.data.get('user_email_id').strip()
    commentNews.parent_comment = request.data.get('parent_comment').strip()
    commentNews.save()
    subscribed_user = models.SubscribedUsers.objects.create(username=request.data.get('user_name').strip(),email=request.data.get('user_email_id').strip())
    news = models.News.objects.get(id=int(newsId))
    news.comments.add(commentNews)
    data ={
     'status':'success'
     }
else:
    data ={
    'status':'failure'
    }
responseData['result'].append(data)
return Response(responseData,status=status.HTTP_200_OK) 
</code></pre>

<p>For further info read the the <a href="http://www.django-rest-framework.org/api-guide/requests/" rel="nofollow">docs here</a></p>

python json django python-2.7