mitmproxy: See and Replay Exactly What Your App or SDK Sends

Stop guessing what your SDK or app really sends. Inspect, record, replay and deliberately break HTTPS traffic with mitmproxy, including reverse proxy mode.

5 min read
AI-assisted

Author's AI disclosure

The author reports using AI to help with this article.

The author confirms reviewing this article.

mitmproxy: See and Replay Exactly What Your App or SDK Sends

The documentation says one thing. The SDK does another. A mobile app talks to an API you can't see. A request fails in production and nobody can say what the client actually sent.

When that happens, stop reading docs and look at the wire. mitmproxy is a proxy that sits between a client and the internet, decrypts HTTPS (once the client trusts its certificate) and lets you inspect every request and response, save them, replay them, and change them on the fly.

flowchart LR
    A["App, SDK or script"] -->|"HTTPS"| B["mitmproxy: decrypt, record, modify"]
    B -->|"HTTPS"| C["Real API"]
    B --> D["Saved flows file"]
    D -->|"client replay"| C
    D -->|"server replay"| A

What it's good for

  • Seeing the truth: the real headers, bodies, redirects, retries and response codes, not what the docs or the client library claim.

  • Reproducing bugs: record a failing session once, then replay it as often as you like.

  • Working offline: answer from a recording instead of the live API.

  • Breaking things on purpose: inject errors, slowness or unexpected values to see how your client copes.

Only intercept traffic from your own devices, apps and accounts.

Install and start

brew install mitmproxy         # macOS
pipx install mitmproxy         # Linux, or anywhere pipx works

There are three front-ends to the same engine:

  • mitmproxy: a terminal UI.

  • mitmweb: a browser UI. Easiest to start with.

  • mitmdump: a command-line version ("tcpdump for HTTP"), best for recording and scripting.

mitmweb --listen-host 127.0.0.1

By default the proxy listens on port 8080 and the web interface on port 8081. Binding to 127.0.0.1 keeps other machines on your network from using your proxy, which matters on shared Wi-Fi. Leave that flag out only when you need a phone to connect.

On first run, mitmproxy creates its own certificate authority in ~/.mitmproxy/. A client can only be inspected if it trusts that CA. Treat the files there as private: mitmproxy-ca.pem contains the CA's private key.

Point a client at it

curl:

curl -x http://127.0.0.1:8080 --cacert "$HOME/.mitmproxy/mitmproxy-ca-cert.pem" https://api.example.com/v1/items

Python (requests):

export HTTPS_PROXY=http://127.0.0.1:8080
export REQUESTS_CA_BUNDLE="$HOME/.mitmproxy/mitmproxy-ca-cert.pem"
python my_script.py

Node.js (v22.21+ or v24+, where the built-in fetch, http and https can read proxy settings from the environment):

NODE_USE_ENV_PROXY=1 \
HTTPS_PROXY=http://127.0.0.1:8080 \
NODE_EXTRA_CA_CERTS="$HOME/.mitmproxy/mitmproxy-ca-cert.pem" \
node app.js

Older Node versions, and libraries such as node-fetch, ignore these variables. They need an explicit proxy agent. Node reads NODE_EXTRA_CA_CERTS only at startup, so set it before launching the process.

Browsers and phones: set the system or Wi-Fi proxy to your computer's address on port 8080, then visit mitm.it through the proxy to download the certificate.

  • iOS: install the profile, then turn on full trust under Settings > General > About > Certificate Trust Settings.

  • Android: since Android 7, apps ignore user-installed certificates by default, so most apps can't be inspected unless they opt in, you use an emulator or rooted device with a system certificate, or you patch your own debug build.

  • Certificate pinning: apps that pin their server certificates refuse to connect through any proxy. That's the pinning working as designed. Don't try to get around it for apps you don't own.

Don't switch off TLS verification (verify=False and similar) to make this work. Trust the mitmproxy CA instead, and unset the variables when you're done.

Read what it sees

In mitmweb, click a flow to see the full request and response. Use the filter box to narrow the list. A few expressions:

Filter

Matches

~d api.example.com

requests to that domain

~m POST

POST requests

~c 500

responses with status 500

~u /v1/orders

URLs matching that pattern

Combine them with &, | and !. Press ? in the terminal UI for the full list.

Things worth checking: headers the client actually sends (User-Agent, Accept-Encoding, auth format), the real body shape, duplicate or retried requests, redirect chains, and which responses are slow.

Record and replay

mitmdump can write everything that passes through to a file:

mitmdump -w session.flows

Then reuse the recording:

# Filter a recording: keep only the POST requests
mitmdump -nr session.flows -w posts.flows "~m post"

# Client replay: re-send the recorded requests (-n means "don't start a proxy")
mitmdump -nC session.flows

# Server replay: act as the server and answer from the recording
mitmdump -S session.flows
  • Client replay sends the recorded requests one at a time, which is useful for reproducing a bug against a fixed server or a staging environment. Add -w result.flows to save what came back.

  • Server replay lets your client run against a recorded "API" with no network. By default requests are matched on URL and method, and each recorded response is served once. Requests with no match go to the real server unless you turn on the option that blocks them. Run mitmdump --options to see the server-replay settings for your version.

Break things on purpose

An addon is a Python file with functions named after events. Run it with mitmdump -s chaos.py.

Fail some requests, to see what your client does with a 503:

# chaos.py
import logging
import random

from mitmproxy import http


def request(flow: http.HTTPFlow) -> None:
    if flow.request.pretty_host != "api.example.com":
        return
    if random.random() < 0.2:   # roughly 1 in 5 requests
        flow.response = http.Response.make(
            503,
            b'{"error": "service unavailable"}',
            {"Content-Type": "application/json"},
        )
        logging.info("injected 503 for %s", flow.request.path)

Setting flow.response inside the request hook answers the client directly, and the request never reaches the real server.

Send an unexpected value, to test how your client handles API drift:

# drift.py
import json

from mitmproxy import http


def response(flow: http.HTTPFlow) -> None:
    content_type = flow.response.headers.get("content-type", "")
    if flow.request.path.startswith("/v1/orders") and "application/json" in content_type:
        data = json.loads(flow.response.text)
        if isinstance(data, dict):
            data["status"] = "a_status_the_client_has_never_seen"
            flow.response.text = json.dumps(data)

Add latency: use an async hook so the proxy keeps serving other requests while it waits:

# slow.py
import asyncio

from mitmproxy import http


async def request(flow: http.HTTPFlow) -> None:
    await asyncio.sleep(2)

No certificate setup: reverse proxy mode

If an SDK lets you change its base URL, you can skip the CA entirely. Run mitmproxy as a reverse proxy in front of the real API:

mitmdump --mode reverse:https://api.example.com@9000 -w session.flows

Then point the SDK at http://localhost:9000. The client talks plain HTTP to your machine, and mitmproxy talks HTTPS to the real API, so there's nothing for the client to trust.

Gotchas

  • Flow files are secrets. They contain every Authorization header, cookie, API key and any personal data in the bodies. Don't commit them or paste them into tickets without redacting them first.

  • Protect the CA. Anyone who gets ~/.mitmproxy/mitmproxy-ca.pem can impersonate sites to any machine that trusts it. Remove the certificate from your trust stores when you're finished.

  • mitmproxy and mitmweb keep every flow in memory. For long sessions, use mitmdump -w.

  • Some clients ignore proxy settings. If nothing shows up, check whether the tool honors HTTPS_PROXY at all.

  • Don't leave an open proxy running. Binding to all interfaces on a shared network lets other people route traffic through your machine.

  • Interception is on you. Use it on traffic you're authorized to inspect.

Wrap-up

When the docs and the behavior disagree, the wire wins. Start with mitmweb and one client, look at a single real request, and you'll usually find the answer within minutes. Once you trust it, record a session with mitmdump -w and keep it as a replayable test fixture, with the secrets removed.

Comments0

Log in to join the discussion.

No comments yet.

Anshu Pathak

Read articles by this author.

Subscribe to Anshu Pathak's newsletter

Get the articles this author chooses to send by email.