Comments (7)
Yes.
from flask-accept.
Hi, can you provide a minimal example?
from flask-accept.
Here is where I created a function and defined the @accept decorator
@accept("application/json")
@demo_bp.route('/server_check', methods=['GET'])
def check_server_health():
"""
This method is just for checking server health and endpoints working
:return: A simple string message
"""
return send_success_response(message="Server is working OK")
When I can this endpoint from postman or cUrl with let's say "text/html" then It will send me error in HTML format, as I think you have raised NotAcceptable
exception in code
So it will show,
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>406 Not Acceptable</title>
<h1>Not Acceptable</h1>
<p>The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request. Supported entities are: application/json</p>
Now If I want the response in JSON in this case as I am using it with APIs so front end will expect to be it in Json with maybe a response like this one.
{error: "The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request. Supported entities are: application/json"}
Can I do it, or it will be always in HTML?
from flask-accept.
OK, so here's an actual minimal example:
from flask import Flask, jsonify
from flask_accept import accept
app = Flask(__name__)
@app.route('/', methods=['GET'])
@accept("application/json")
def check():
return "Server is working OK"
app.run('0.0.0.0', 8080, debug=True)
If I run this locally, I get:
$ curl localhost:8080
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>406 Not Acceptable</title>
<h1>Not Acceptable</h1>
<p>The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request. Supported entities are: application/json</p>
To make that a JSON response instead, you need to provide a custom error handler, for example:
from flask import jsonify
@app.errorhandler(406)
def not_acceptable(e):
return jsonify({'error': 'Not Acceptable'}), 406
Which will give you:
$ curl localhost:8080
{
"error": "Not Acceptable"
}
from flask-accept.
So How would the library fit in this case? So in your example, the accept decorator is not added. So I am confused about how it will work? so let's say using the library instead of sending the HTML response. What I intended to do is
{"message": "The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request. Supported entities are: application/json"}
using the accept library itself and not adding one more error handler, In your JSON example you have not used the library at all
from flask-accept.
I think you need to take a closer look at the example 🙂
from flask-accept.
okay so If I override the 406 at app handler level then accept would automatically follow what that function is sending, right?
from flask-accept.
Related Issues (9)
- Support Python 3
- Wildcard media type handling HOT 2
- Unable to decorate routes within a Flask_RESTful Resource class
- Multiple resources using Flask Restful HOT 1
- Flask-REST Marshmallow Two-way Nesting Schemas HOT 1
- Couldn't get it to work HOT 12
- Flask `MethodView` and `flask-smorest` HOT 4
- Error generating Swagger with flask-restx 0.3.0 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flask-accept.