requests.get(url).json() gives JSONDecodeError
Question: I am writing an api to get the data of an app in another app. I have my views
setup to get the data from the url like:

    
    
    import requests
    user = 'hello'
    pwd = 'python'
    class SomeView(APIView):
        def get(self, request):
            if request.user.is_authenticated():
                r = requests.get('http://localhost:8000/foo/bar/', 
                    auth=HTTPBasicAuth(user, pwd))
                return HttpResponse(r.json())
            else:
                return HttpResponse(json.dumps({'success':'false', 'message':'login required '}))
    

This gives me error like:

    
    
    Traceback (most recent call last):
    File "<console>", line 1, in <module>
    File "/home/abhishek/Documents/venv/local/lib/python2.7/site-packages/requests/models.py", line 799, in json
    return json.loads(self.text, **kwargs)
    File "/home/abhishek/Documents/venv/local/lib/python2.7/site-packages/simplejson/__init__.py", line 505, in loads
    return _default_decoder.decode(s)
    File "/home/abhishek/Documents/venv/local/lib/python2.7/site-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
    File "/home/abhishek/Documents/venv/local/lib/python2.7/site-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
    File "/home/abhishek/Documents/venv/local/lib/python2.7/site-packages/simplejson/scanner.py", line 127, in scan_once
    return _scan_once(string, idx)
    File "/home/abhishek/Documents/venv/local/lib/python2.7/site-packages/simplejson/scanner.py", line 118, in _scan_once
    raise JSONDecodeError(errmsg, string, idx)
    JSONDecodeError: Expecting value: line 1 column 1 (char 0)
    

I have django == 1.4.5 and requests == 2.5.1 installed in my virtual
environment. I have checked almost everything and now i am starting to
conclude that the requests version and django version have something to do
with the following traceback. I also have simplejson==3.6.5 installed in my
virtual environment, which i think has no relevance. Help Please.


Answer: you can do something like this

    
    
    import requests
    from rest_framework.response import Response
    ...
    
    if request.user.is_authenticated():
        r = requests.get('http://localhost:8000/foo/bar/', 
                         auth=HTTPBasicAuth(user, pwd))
        return Response(r.json())
    return Response({'success':'false', 'message':'login required '})
    

