About — Flask Routes

This page shows simple examples of Flask routes and how to call them.

1) Basic route (GET)

from flask import Flask
app = Flask(__name__)

@app.route('/hello')
def hello():
    return 'Hello, world!'

2) Route with methods (POST example)

@app.route('/api/display', methods=['POST'])
def display_text():
    data = request.get_json()
    return jsonify({'text': data.get('text', '')})

How to call the POST route

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))

3) Route variables

@app.route('/user/<username>')
def profile(username):
    return f"Profile: {username}"

That's enough to get started. Return to home.