Simple Flask API Setup

This snippet demonstrates a basic setup of a Flask API with a custom method view. It includes handlers for GET and POST requests, providing a structured response. Perfect for learning how to create RESTful APIs in Python.
 avatar
unknown
python
6 months ago
459 B
2
Indexable
from flask import Flask, jsonify, request, views
import sys
app = Flask(__name__)


class CustomFlaskAPI(views.MethodView):
    def post(self):
        print(str(request.json), flush=True)
        return jsonify({}), 201
    def get(self):
        return jsonify({ "success": True }), 201

app.add_url_rule('/test-api', view_func=CustomFlaskAPI.as_view('api'))

if __name__ == "__main__":
    print("Starting test server")
    app.run(debug=False, port=8083)
Editor is loading...
Leave a Comment