In Python, you can expose a REST API using various frameworks, with Flask and FastAPI being some of the most popular choices. Below is an example using Flask, which is lightweight and easy to use.
To create a simple REST API using Flask, you need to first install Flask if you haven't already:
pip install Flask
Next, you can create a basic API that handles GET and POST requests:
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route('/api/items', methods=['GET'])
def get_items():
items = {"items": ["item1", "item2", "item3"]}
return jsonify(items)
@app.route('/api/items', methods=['POST'])
def add_item():
new_item = request.json.get('item')
# Here you would typically add the new item to a database
return jsonify({"message": f"Item {new_item} added!"}), 201
if __name__ == '__main__':
app.run(debug=True)
This example creates an API with two endpoints:
Once you run this script, you can test the API endpoints using tools like Postman or cURL.
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?