Marcio Cunha

How to inspect and debug HTTP and HTTPS traffic with mitmproxy

Learn how to configure mitmproxy to intercept, debug, and modify web traffic in real-time, giving you full control over your development environment.

Marcio Cunha•2 min
Also available in:PortuguêsEspañol
Summary
  • Mitmproxy acts as a transparent intermediary to inspect requests and responses between your client and server.
  • Configuring custom SSL/TLS certificates is the most critical step to avoid security warnings when intercepting HTTPS.
  • Modifying packets in transit allows developers to simulate edge cases and API errors without changing application code.
  • Standard network tools like tcpdump or monitoring traffic with tcpdump complement the high-level request inspection.
  • Automated scripts can manipulate traffic flows dynamically, enabling complex test scenarios for API integrations.

Why use an interception proxy?

During web development, we often face the mystery of what exactly is being sent to an API. Mitmproxy is a command-line tool that acts as an intermediary between your computer and the internet, allowing you to peek inside your connections. Unlike a browser's built-in inspector, it captures traffic before it is rendered, which is perfect for debugging server requests, backend scripts, or mobile applications.

Installation and setup

Mitmproxy is lightweight and easily installed on Linux or macOS. Ensure you have Python installed, as it is distributed via pip. The default installation provides three interfaces: mitmproxy (interactive terminal), mitmweb (browser-based GUI), and mitmdump (scripting version). In practice, using the mitmweb interface is the most friendly approach for beginners, as it provides a clear view of all requests.

Intercepting HTTPS traffic

The most complex part of intercepting traffic today is HTTPS. Since HTTPS is designed to prevent eavesdropping, mitmproxy needs to act as a 'Man-in-the-Middle', generating fake certificates that your computer must trust. When you run mitmproxy for the first time, it creates its own certificate authority. You must install this certificate into your system's trusted root store. Without this step, your browser will flag any site you visit as insecure, as it detects the connection is being intercepted.

Modifying packets in real-time

One of the most powerful features of mitmproxy is the ability to pause a request, edit its content (like changing a JSON parameter or an HTTP header), and forward it to the destination. This is invaluable for testing how your API reacts to malformed data or validating business rules without changing your source code. If you need to measure network performance during these exchanges, consider how to use iperf3 to diagnose container network bottlenecks.

Automation with scripts

Beyond manual manipulation, mitmproxy allows you to write Python scripts to automate tasks. Imagine you need to test a payment flow that requires a specific field in the response to be changed every time. With a simple script, you can intercept all 200 OK responses, inject the required data, and pass it to the client. This makes the development environment much faster and more predictable. Think of mitmproxy as a core tool, much like the precision needed to test zener diodes during hardware repairs.

Final considerations

Mastering mitmproxy improves your troubleshooting skills significantly. It bridges the gap between 'I think the server sent X' and the mathematical certainty of what happened at the protocol level. Always remember to remove the trusted certificate authority from your computer after finishing your debugging work to avoid long-term security risks.

By incorporating this tool into your workflow, you gain the ability to audit complex integrations with the same clarity that a code debugger offers for application logic. It is an essential skill for any developer working with distributed systems or APIs that require rigorous validation.