Junior — Middle
How are REST API principles implemented when developing in Python?
sobes.tech AI
Answer from AI
REST API principles in Python development are implemented by adhering to REST architectural constraints:
- Using HTTP methods (GET, POST, PUT, DELETE) for resource operations.
- Identifying resources via URLs.
- Statelessness on the server.
- Using standard HTTP response codes.
Frameworks such as Flask or Django REST Framework are often used for implementation.
Example of a simple REST API in Flask:
from flask import Flask, jsonify, request
app = Flask(__name__)
items = []
@app.route('/items', methods=['GET'])
def get_items():
return jsonify(items)
@app.route('/items', methods=['POST'])
def add_item():
item = request.json
items.append(item)
return jsonify(item), 201
if __name__ == '__main__':
app.run()
This approach provides a standardized and convenient interface for client interaction.