0

Consider the following Django REST view:

class TestList(APIView):
    renderer_classes = [TemplateHTMLRenderer, JSONRenderer]
    template_name = 'android/test.html'

    def get(self, request):
        queryset = Test.objects.all()
        return Response({'test_qs': queryset})

The URLConf:

...
url(r'^test/$', test_views.TestList.as_view(), name = 'test'),
...

I designed the view implying that it would serve both regular webpages and API responses. The page is perfectly ok if I call it from browser, I see the expected template android/test.html. Now the problem arises when I try to call the url from cURL specifying json content-type in Accept header. I tried two options.

curl --header "application/json" http://127.0.0.1:8000

which still returns me the webpage template HTML code in place of JSON seemingly ignoring the --header parameter

and

curl -i -H "Accept: application/json" http:127.0.0.1:8000/test/ 

which results in a long-stated error. Because I can't copy-paste it from cmd, I'll just attach the screenshot here. enter image description here Any ideas? Is the error lies in cURL request or my API itself ?

Edgar Navasardyan
  • 4,261
  • 8
  • 58
  • 121
  • you are saying when using `"Accept: application/json"` your you see error message or what? – aliva Dec 13 '16 at 08:59
  • Yes, and the error is so long that the previous command in the cmd becomes invisible) There's some strange HTML table rendered and in the end, they say that I am saying this error because I am using Django in debug mode. I'll attach the screenshot of the error in the question – Edgar Navasardyan Dec 13 '16 at 09:03
  • 1
    when you want to request json data you have to add `"Accept: application/json"` as header so your second request is right, there is some other problems with your code, try redirecting output into a file http://stackoverflow.com/a/1420981/180960 – aliva Dec 13 '16 at 09:08

1 Answers1

0

It doesn't looks relating to content types. You seems to have a 500 error along with DEBUG=True.

You should look at the run server logs, get the traceback and fix the error that is causing the 500 code.

Linovia
  • 19,812
  • 4
  • 47
  • 48