Skip to content
dreamcode
dreamcode
Map
Requests + responses
Lesson 72 of 77
+15 XP on finish
PYTHON APPLIEDChapter 12 · Python for Web Development

Handling HTTP requests and responses

Every web request carries data: query parameters in the URL, headers with metadata, and optionally a body with JSON or form data. Your backend reads these inputs, processes them, and returns a response with a status code indicating success or failure.

Worked example
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/api/search")
def search():
    query = request.args.get("q", "")
    limit = request.args.get("limit", 10, type=int)

    results = [s for s in ["Vega", "Sirius", "Venus"]
               if query.lower() in s.lower()]

    return jsonify({
        "query": query,
        "results": results[:limit],
        "count": len(results),
    }), 200

How it reads

  • request.args.get('q') reads a query parameter from the URL
  • 200 is the HTTP status code meaning the request succeeded
  • The response body is a JSON object with query, results, and count
Cloud tip: Common status codes: 200 OK, 201 Created, 400 Bad Request, 404 Not Found, 500 Internal Server Error.

Check your understanding

0 / 3

Answer all 3 to complete this lesson and earn 15 XP.

  1. 1. How do you read a URL query parameter named 'q' in Flask?
  2. 2. What HTTP status code means 'resource not found'?
  3. 3. What part of an HTTP request typically carries JSON data?
Answer every question to unlock the next lesson.