general

Middleware

Middleware is software that sits between a scraping framework's core request/response cycle and user-defined handlers, adding cross-cutting behaviour like logging, retry, or rate limiting.

In web frameworks and scraping tools, middleware is a chain of components through which every request and response passes. Each middleware component can inspect or modify the request before it is sent, and the response after it is received. Middleware is ideal for cross-cutting concerns that apply uniformly across all requests: adding authentication headers, logging request metadata, measuring latency, rotating proxies, decompressing responses, or applying retry logic.

Scrapy's spider middleware and downloader middleware pipelines are a canonical example. Scrapy passes each request through the downloader middleware chain before sending it and passes each response through the same chain in reverse on the way back, allowing middleware to transform both requests and responses declaratively.

In FastAPI (used by AlterLab's API layer), middleware intercepts HTTP requests and responses, enabling authentication checking, request ID injection, CORS headers, and timing instrumentation without modifying individual route handlers.

Examples

# Scrapy: custom downloader middleware that rotates proxies
class ProxyRotationMiddleware:
    def process_request(self, request, spider):
        request.meta["proxy"] = get_next_proxy()
        return None  # continue processing

Related Terms

Extract Middleware data from any website

AlterLab returns clean, structured data from any public URL — no scraper infrastructure needed. Start free, no credit card required.

View API docs

Your first scrape.
Sixty seconds.

$1 free balance. No credit card. No SDK.Just a POST request.

terminal
curl -X POST https://api.alterlab.io/v1/scrape \
-H "X-API-Key: YOUR_KEY" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com", "formats": ["markdown"]}'

No credit card required · Up to 5,000 free scrapes · Balance never expires

    Middleware — Web Scraping Glossary | AlterLab