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

Handle JSON default renderer and empty response body. #489

Open
jenstroeger opened this issue May 28, 2018 · 5 comments
Open

Handle JSON default renderer and empty response body. #489

jenstroeger opened this issue May 28, 2018 · 5 comments
Labels

Comments

@jenstroeger
Copy link
Collaborator

jenstroeger commented May 28, 2018

The following is more of a question and observation, with a related discussion in the Pylons group, and perhaps somewhat related is issue #470.

Here’s the thing:

@user_logout.post(
    content_type="application/json",
    accept="application/json",
    permission="logout",
    )
def user_logout_post(request):
    """Log out and invalidate the session token."""
    […]
    return HTTPOk()

Here the view returns a new HTTPOk() object (which is also an HTTPException which is also a Response); it doesn’t use the request.response object. Because of that, it seems that the default JSON renderer is not applied and the response is an HTML content type, thus contradicting the request’s Accept header.

In order to ensure a JSON response here, the view should return like so:

    # return HTTPOk()
    request.response.status = 200  # Defaults to 200, but could be 201 etc.
    return None

Returning None here causes the response to be rendered by the default JSON renderer which is what I want. However, returning None also adds a null response body, which is not what I want.

What’s the recommended way out of this dilemma? How can I use the default JSON renderer and return empty JSON responses? Is this currently doable, or would that need a Cornice patch?

@jenstroeger
Copy link
Collaborator Author

@leplatrem, why the 😕? Question/issue not clear?

@leplatrem
Copy link
Contributor

leplatrem commented Jun 5, 2018

I'm sorry, no! I was just completely unsure of what to do about it :/

@jenstroeger
Copy link
Collaborator Author

Is this the code blob where the view’s response would be checked?

@leplatrem
Copy link
Contributor

Yes !

cornice/cornice/service.py

Lines 500 to 502 in ded8422

response = view_()
else:
response = view_(request)

@leplatrem
Copy link
Contributor

Now that I read the code again for fixing #521, I believe that we could check the type of the returned object, and if it's an instance of Reponse then just use it. No?

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

No branches or pull requests

2 participants