How to capture HTTP request / response headers with mitmproxy?

sfactor picture sfactor · Jul 3, 2015 · Viewed 11.2k times · Source

I have been able to capture the HTTP(s) traffic from a smartphone and also stored this traffic using mitmdump using the command

mitmdump -w outfile

This seems to dump the HTTP body along with the headers as well. I am interested in capturing only the headers, prefarably as a single csv row (or json string). How can I do that?

Answer

user553965 picture user553965 · Jul 11, 2017

Yet another derived snippet based on previous responses and updated to python3:

def response(flow):
    print("")
    print("="*50)
    #print("FOR: " + flow.request.url)
    print(flow.request.method + " " + flow.request.path + " " + flow.request.http_version)

    print("-"*50 + "request headers:")
    for k, v in flow.request.headers.items():
        print("%-20s: %s" % (k.upper(), v))

    print("-"*50 + "response headers:")
    for k, v in flow.response.headers.items():
        print("%-20s: %s" % (k.upper(), v))
        print("-"*50 + "request headers:")

Command line:

mitmdump -q -v -s parse_headers.py -R http://localhost:9200 -p 30001

Output:

==================================================
GET / HTTP/1.1
--------------------------------------------------request headers:
CONTENT-TYPE        : application/json
ACCEPT              : application/json
USER-AGENT          : Jakarta Commons-HttpClient/3.1
HOST                : localhost
--------------------------------------------------response headers:
CONTENT-TYPE        : application/json; charset=UTF-8
CONTENT-LENGTH      : 327