Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONRenderer does not return a valid json response when data=None #6026

Closed
kltdwrds opened this issue Jun 13, 2018 · 4 comments
Closed

JSONRenderer does not return a valid json response when data=None #6026

kltdwrds opened this issue Jun 13, 2018 · 4 comments

Comments

@kltdwrds
Copy link

kltdwrds commented Jun 13, 2018

Steps to reproduce

@permission_classes((AllowAny, ))
class Viewset(viewsets.ViewSet):
    renderer_classes = (JSONRenderer, )
    def list(self, request, *args, **kwargs):
        return Response()

Expected behavior

A valid json response body like null or {}

Actual behavior

No Content-Type header set and an empty response is returned - particularly painful when making requests with jquery's ajax(). It throws an parseerror and rejects the promise on a success response with an empty body. See here https://stackoverflow.com/a/6186905

PR here: #6027

@rpkilby
Copy link
Member

rpkilby commented Jun 13, 2018

Just a quick clarification.. I may not be recreating your exact setup, but a quick, manual test shows that the Content-Type header is correctly set to application/json.

As to the empty response, I'm inclined to agree with @kltdwrds. I don't know if the spec allows an empty body, however, both JavaScript's JSON.parse and Python's json.loads fail to decode an empty string.

That is to say, data=None should be encoded as null.


On second thought, the current behavior makes sense. Although JSON.parse and json.loads do fail, it's the responsibility of the client to check the content type before parsing. If anything, this is a bug in jQuery.

@kltdwrds kltdwrds changed the title Jsonrenderer does not return a valid json response when data=None JSONRenderer does not return a valid json response when data=None Jun 13, 2018
@kltdwrds
Copy link
Author

hmm I'm using

from rest_framework import viewsets
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response

Not sure where the Content-Type header gets set, but I just confirmed it missing from responses using the example above.

@kltdwrds
Copy link
Author

It looks like the Content-Type header is deleted on an empty renderer response (line 80)
https://github.com/encode/django-rest-framework/blob/master/rest_framework/response.py#L72

@tomchristie
Copy link
Member

Our current behavior for return Response() is intentional. Although returning null would also be valid, it's more typical that we need a way to return an empty response with no content type. I'd probably rather we leave things as they are for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants