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

Serializing flask response #31

Closed
mekanix opened this issue Nov 25, 2018 · 6 comments
Closed

Serializing flask response #31

mekanix opened this issue Nov 25, 2018 · 6 comments
Labels

Comments

@mekanix
Copy link

mekanix commented Nov 25, 2018

I need to set cookies with on some endpoints. Just returning jsonify({ 'key': 'value' }) does not get serialized. Is there a way to set a cookie in the response together with the "normal" endpoint response?

@lafrech
Copy link
Member

lafrech commented Nov 29, 2018

Can you elaborate on your use case?

Since this is about REST APIs, I assume it is an authentication cookie, right?

Do you want to set it from the view function? From a custom authentication decorator?

@mekanix
Copy link
Author

mekanix commented Nov 30, 2018

Yes, it's auth cookie. So, with flask_restplus this is the code I used: https://github.com/mekanix/backend-startkit/blob/master/application/api/auth.py#L21-L64. Something I tried to achieve with flask_rest_api is this: https://github.com/mekanix/backend-startkit/blob/feature/rest-api/application/api/auth.py#L24-L68. The problem is that frontend says length of response is wrong (I suppose I'm overwriting something).

@lafrech
Copy link
Member

lafrech commented Feb 12, 2019

I don't see anything blocking you from setting a cookie from a decorator.

However, setting a cookie from a view function is currently impossible because by design of the response method, the view function can not return a flask Response object. This is discussed in #22.

IIUC, the return resp.get_json() in your code means you're returning only the json body of the response and not the cookies.

@mekanix
Copy link
Author

mekanix commented Feb 13, 2019

Than decorator is the right place. :) Can you please post the code snippet to achieve it? The code I linked to is not the way I have to solve it, it just the closest I've got to setting the cookie, so feel free to just ignore it.

@lafrech
Copy link
Member

lafrech commented Feb 13, 2019

I have no available snippet and no time for this right now.

You might find the conversation in #36 interesting, especially this comment.

@lafrech
Copy link
Member

lafrech commented Apr 11, 2019

Closing this for now. Feel free to comment if you're still stuck on this.

@lafrech lafrech closed this as completed Apr 11, 2019
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