Mitmproxy: how can I save only request and response json body

Created on 18 Aug 2016  路  2Comments  路  Source: mitmproxy/mitmproxy

Hi,
I want to save all request and response JSON body, how can I do that?
Thanks in advance.

kinquestion

Most helpful comment

There's a keyboard shortcut (E key) to save body, but it only works on a single flow, but mitmproxy provides a neat scripting API that has you covered in such cases. This, IMHO, is where mitm one-ups all other proxy tools.

Save this script as body_saver.py:

def response(flow):
    with open("file_to_write_data_to.txt", "ab") as ofile:
        ofile.write(flow.request.pretty_url)

        if flow.request.content:
            ofile.write(flow.request.content)

        if flow.response.content:
            ofile.write(flow.response.content)

        # Add other separators etc. however you want
        ofile.write(b"-------")

Then run, mitmdump -s body_saver.py and let all your requests pass through. file_to_write_data_to.txt will be created in your current directory with the data.

To learn more about the scripting API, see the docs: http://docs.mitmproxy.org/en/stable/scripting/inlinescripts.html

P.S: For these question type issues we like to use discourse: http://discourse.mitmproxy.org/ :smile:

All 2 comments

There's a keyboard shortcut (E key) to save body, but it only works on a single flow, but mitmproxy provides a neat scripting API that has you covered in such cases. This, IMHO, is where mitm one-ups all other proxy tools.

Save this script as body_saver.py:

def response(flow):
    with open("file_to_write_data_to.txt", "ab") as ofile:
        ofile.write(flow.request.pretty_url)

        if flow.request.content:
            ofile.write(flow.request.content)

        if flow.response.content:
            ofile.write(flow.response.content)

        # Add other separators etc. however you want
        ofile.write(b"-------")

Then run, mitmdump -s body_saver.py and let all your requests pass through. file_to_write_data_to.txt will be created in your current directory with the data.

To learn more about the scripting API, see the docs: http://docs.mitmproxy.org/en/stable/scripting/inlinescripts.html

P.S: For these question type issues we like to use discourse: http://discourse.mitmproxy.org/ :smile:

Thanks for this @dufferzafar. Note that with Python 3.x, the above snippet no longer works, you will get errors like this:

Addon error: Traceback (most recent call last):
  File "body_saver.py", line 3, in response
    ofile.write(flow.request.pretty_url)
TypeError: a bytes-like object is required, not 'str'

The solution is to change the first ofile.write() invocation like this (i.e. add the .encode() call to the end of the line):

        ofile.write(flow.request.pretty_url.encode())
Was this page helpful?
0 / 5 - 0 ratings