-
Notifications
You must be signed in to change notification settings - Fork 73
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
No way to set custom headers #18
Comments
Valid point. flask-rest-api uses the application context to pass header informations from one decorator to the other. You can access the application context using I could add the same return value handling as in Flask, allowing to return tuples from view funcs. This way, headers could be returned from the view function. Using the same mechanism, the view function could also return a status code. This would allow a view function to override the code defined in the I need to think this through. It looks like a nice improvement, but it's an important change in the logic of the code. Until then, you can use BTW, the |
Hi! I think this is related to the issue. It seems like there is no way to fully modify response data structure. There is
in case of my internal exception
Right now I'm thinking about a hack - override def response(self, *args, **kwargs):
decorator = super().response(*args, **kwargs)
def next_decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
r = decorator(func(*args, **kwargs))
return r
except AppException as e:
resp = jsonify(self._prepare_response_content({
'status': 'error',
'message': str(e)
}))
resp.headers.extend(get_appcontext()['headers'])
resp.status_code = e.status_code
return wrapper
return next_decorator Thanks! |
@ocryle, for this use case, I'd abort with a sensible error code and message. abort(422, "Foo is not a valid Bar") Then if I'm not happy with the default error structure, I'd override (I just realized the typo in the name s/reponse/response. I shall fix that asap. Edit: Fixed in master.) This means all your errors look the same. I agree this is a constraint, but I think it is nice from client POV. And it's nice in your views because you just need one call to abort. |
Yeah, that seems to be a much better way to do this. Thank you! |
@dryobates, I just sent a PR fixing this (#40). Feedback welcome. |
@lafrech Thanks. It looks great. I have only one problem with this approach - transition time. Now I use get_context()["headers"] ;) But as it's undocumented and as I have to adjust to 0.13.0 other's backward incompatible changes that's not big deal :) Thank you very much. New API is beautifully symmetrical with flask's own :) |
In regular flask function I could return headers as last parameter:
return body, status_code, headers
That's how I return e.g. "Location" header.
In flask-rest-api I can't return such tuple nor Response object with custom headers because Api.response requires object to serialize.
I see that there is some headers handling in code https://github.com/Nobatek/flask-rest-api/blob/master/flask_rest_api/response.py#L74 but I do not know how to elegantly set them in my function.
If there's no elegant way then maybe handling return value as in flask (https://github.com/pallets/flask/blob/master/flask/app.py#L1890) should be implemented?
The text was updated successfully, but these errors were encountered: