Navigation

We realize that everyone has different needs in a REST framework.
Flask-RESTful tries to be as flexible as possible, but sometimes you might
find that the builtin functionality is not enough to meet your needs.
Flask-RESTful has a few different extension points that can help in that case.

Out of the box, Flask-RESTful is only configured to support JSON. We made this
decision to give API maintainers full control of over API format support; so a
year down the road you don’t have to support people using the CSV
representation of your API you didn’t even know existed. To add additional
mediatypes to your API, you’ll need to declare your supported representations
on the Api object.

Flask-RESTful uses the json module from the Python standard library
instead of flask.json because the Flask JSON serializer includes
serializtion capabilities which are not in the JSON spec. If your
application needs these customizations, you can replace the default JSON
representation with one using the Flask JSON module as described above.

It is possible to configure how the default Flask-RESTful JSON representation
will format JSON by providing a RESTFUL_JSON attribute on the application
configuration. This setting is a dictionary with keys that correspond to the
keyword arguments of json.dumps().

If the application is running in debug mode (app.debug=True) and
either sort_keys or indent are not declared in the RESTFUL_JSON
configuration setting, Flask-RESTful will provide defaults of True and
4 respectively.

data is the object you return from your resource method, code is the HTTP
status code that it expects, and headers are any HTTP headers to set in the
response. Your output function should return a flask.Response object.

defoutput_json(data,code,headers=None):"""Makes a Flask response with a JSON encoded body"""resp=make_response(json.dumps(data),code)resp.headers.extend(headersor{})returnresp

Another way to accomplish this is to subclass the Api class and
provide your own output functions.

There is a property on the Resource class called
method_decorators. You can subclass the Resource and add your own
decorators that will be added to all method functions in resource. For
instance, if you want to build custom authentication into every request.

Error handling is a tricky problem. Your Flask application may be wearing
multiple hats, yet you want to handle all Flask-RESTful errors with the correct
content type and error syntax as your 200-level requests.

Flask-RESTful will call the handle_error()
function on any 400 or 500 error that happens on a Flask-RESTful route, and
leave other routes alone. You may want your app to return an error message with
the correct media type on 404 Not Found errors; in which case, use the
catch_all_404s parameter of the Api constructor.

app=Flask(__name__)api=flask_restful.Api(app,catch_all_404s=True)

Then Flask-RESTful will handle 404s in addition to errors on its own routes.

Sometimes you want to do something special when an error occurs - log to a
file, send an email, etc. Use the got_request_exception() method
to attach custom error handlers to an exception.

You may want to return a specific message and/or status code when certain errors
are encountered during a request. You can tell Flask-RESTful how you want to
handle each error/exception so you won’t have to fill your API code with
try/except blocks.

errors={'UserAlreadyExistsError':{'message':"A user with that username already exists.",'status':409,},'ResourceDoesNotExist':{'message':"A resource with that ID no longer exists.",'status':410,'extra':"Any extra information you want.",},}

Including the ‘status’ key will set the Response’s status code. If not
specified it will default to 500.

Once your errors dictionary is defined, simply pass it to the
Api constructor.