Hi,
I want to save all request and response JSON body, how can I do that?
Thanks in advance.
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())
Most helpful comment
There's a keyboard shortcut (
Ekey) 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:Then run,
mitmdump -s body_saver.pyand let all your requests pass through.file_to_write_data_to.txtwill 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: