tanszek:oktatas:iss_t:rest_api
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| tanszek:oktatas:iss_t:rest_api [2026/03/01 17:17] – [Core REST Principles] knehez | tanszek:oktatas:iss_t:rest_api [2026/03/01 17:24] (current) – [Core REST Principles] knehez | ||
|---|---|---|---|
| Line 63: | Line 63: | ||
| { " | { " | ||
| </ | </ | ||
| + | |||
| + | ==== REST vs JSON-RPC ==== | ||
| + | |||
| + | ^ Feature ^ REST ^ JSON-RPC ^ | ||
| + | | Architecture | Resource-based | Procedure-based | | ||
| + | | Endpoints | Multiple | Usually single | | ||
| + | | Uses HTTP verbs | Yes | Not required | | ||
| + | | Standardized by | Architectural constraints | Protocol specification | | ||
| + | | Typical use case | Public APIs | Internal APIs | | ||
| + | |||
| + | REST is typically used for public web APIs, while JSON-RPC is often used in internal systems and blockchain interfaces. | ||
| + | |||
| + | ===== REST API Server (Python + Flask) ===== | ||
| + | |||
| + | In this section, we implement a simple REST API using Flask. | ||
| + | |||
| + | Install dependency: | ||
| + | |||
| + | <code bash> pip install flask </ | ||
| + | |||
| + | Create file: // | ||
| + | |||
| + | <sxh python> | ||
| + | from flask import Flask, jsonify, request | ||
| + | |||
| + | app = Flask(__name__) | ||
| + | |||
| + | users = [ | ||
| + | {" | ||
| + | {" | ||
| + | ] | ||
| + | |||
| + | |||
| + | @app.route("/ | ||
| + | def get_users(): | ||
| + | return jsonify(users) | ||
| + | |||
| + | |||
| + | @app.route("/ | ||
| + | def get_user(user_id): | ||
| + | for user in users: | ||
| + | if user[" | ||
| + | return jsonify(user) | ||
| + | return jsonify({" | ||
| + | |||
| + | |||
| + | @app.route("/ | ||
| + | def create_user(): | ||
| + | new_user = request.get_json() | ||
| + | new_user[" | ||
| + | users.append(new_user) | ||
| + | return jsonify(new_user), | ||
| + | |||
| + | |||
| + | @app.route("/ | ||
| + | def delete_user(user_id): | ||
| + | global users | ||
| + | users = [u for u in users if u[" | ||
| + | return jsonify({" | ||
| + | |||
| + | |||
| + | if __name__ == " | ||
| + | app.run(port=5000) | ||
| + | </ | ||
| + | |||
| + | Run: | ||
| + | |||
| + | <code bash> python server.py </ | ||
| + | |||
| + | ===== Testing with curl ===== | ||
| + | |||
| + | Get all users: | ||
| + | |||
| + | <code bash> curl http:// | ||
| + | |||
| + | Create new user: | ||
| + | |||
| + | <code bash> curl -X POST http:// | ||
| + | |||
| + | Delete user: | ||
| + | |||
| + | <code bash> curl -X DELETE http:// | ||
| + | |||
| + | ==== HTTP Status Codes ==== | ||
| + | |||
| + | REST APIs rely heavily on HTTP status codes: | ||
| + | |||
| + | ^ Code ^ Meaning ^ | ||
| + | | 200 | OK | | ||
| + | | 201 | Created | | ||
| + | | 400 | Bad request | | ||
| + | | 401 | Unauthorized | | ||
| + | | 404 | Not found | | ||
| + | | 500 | Server error | | ||
| + | |||
| + | Status codes are an essential part of REST communication. | ||
| + | |||
| + | ==== Advantages and Limitations ==== | ||
| + | |||
| + | REST is widely adopted, easy to understand, and integrates naturally with HTTP infrastructure. It supports caching, authentication mechanisms, and standard tooling. | ||
| + | |||
| + | However, REST can sometimes be verbose. Complex operations may require multiple endpoints, and over-fetching or under-fetching data can become an issue in large systems. | ||
tanszek/oktatas/iss_t/rest_api.1772385468.txt.gz · Last modified: 2026/03/01 17:17 by knehez
