Giter VIP home page Giter VIP logo

Comments (7)

di avatar di commented on September 15, 2024 1

Yes.

from flask-accept.

di avatar di commented on September 15, 2024

Hi, can you provide a minimal example?

from flask-accept.

TapanHP1995 avatar TapanHP1995 commented on September 15, 2024

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.

di avatar di commented on September 15, 2024

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.

TapanHP1995 avatar TapanHP1995 commented on September 15, 2024

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.

di avatar di commented on September 15, 2024

I think you need to take a closer look at the example 🙂

from flask-accept.

TapanHP1995 avatar TapanHP1995 commented on September 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.