This page shows simple examples of Flask routes and how to call them.
from flask import Flask
app = Flask(__name__)
@app.route('/hello')
def hello():
return 'Hello, world!'
@app.route('/api/display', methods=['POST'])
def display_text():
data = request.get_json()
return jsonify({'text': data.get('text', '')})
Using curl:
curl -X POST http://localhost:5000/api/display \
-H "Content-Type: application/json" \
-d '{"text":"Hello from curl"}'
Using JavaScript fetch:
fetch('/api/display', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: 'Hello from fetch' })
})
.then(r => r.json())
.then(data => console.log(data))
@app.route('/user/<username>')
def profile(username):
return f"Profile: {username}"
That's enough to get started. Return to home.