tanszek:oktatas:iss_t:rest_api
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| tanszek:oktatas:iss_t:rest_api [2026/03/01 17:15] – created knehez | tanszek:oktatas:iss_t:rest_api [2026/03/01 17:24] (current) – [Core REST Principles] knehez | ||
|---|---|---|---|
| Line 9: | Line 9: | ||
| For example: | For example: | ||
| - | < | + | < |
| + | GET / | ||
| + | GET / | ||
| + | </ | ||
| In REST, the client does not call functions explicitly. Instead, it performs operations on resources using standard HTTP methods. | In REST, the client does not call functions explicitly. Instead, it performs operations on resources using standard HTTP methods. | ||
| + | |||
| + | ==== Core REST Principles ==== | ||
| + | |||
| + | === Resource Identification === | ||
| + | |||
| + | Every resource must have a unique identifier, typically a URL. The URL represents the resource, not the action. | ||
| + | |||
| + | Bad example (RPC-style thinking): | ||
| + | |||
| + | < | ||
| + | GET / | ||
| + | </ | ||
| + | |||
| + | === HTTP Methods (Verbs) === | ||
| + | |||
| + | REST relies on standard HTTP methods to define operations: | ||
| + | |||
| + | ^ Method ^ Meaning ^ Example ^ | ||
| + | | GET | Retrieve resource | GET /users/1 | | ||
| + | | POST | Create new resource | POST /users | | ||
| + | | PUT | Replace resource | PUT /users/1 | | ||
| + | | PATCH | Partial update | PATCH /users/1 | | ||
| + | | DELETE | Remove resource | DELETE /users/1 | | ||
| + | |||
| + | The method defines the action, not the URL. | ||
| + | |||
| + | === Statelessness === | ||
| + | |||
| + | Like JSON-RPC, REST is stateless. Each request must contain all necessary information. The server does not store client session state between requests (unless explicitly implemented via tokens or cookies). | ||
| + | |||
| + | This improves scalability and simplifies distributed deployments. | ||
| + | |||
| + | === Representation === | ||
| + | |||
| + | Resources are transferred in representations, | ||
| + | |||
| + | * JSON | ||
| + | * XML | ||
| + | * HTML | ||
| + | * Plain text | ||
| + | |||
| + | Today, JSON is the dominant format in REST APIs. | ||
| + | |||
| + | Example response: | ||
| + | |||
| + | <code json> | ||
| + | { " | ||
| + | </ | ||
| + | |||
| + | ==== 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.1772385302.txt.gz · Last modified: 2026/03/01 17:15 by knehez
