Replace response body with local json file from a list of files

Hey
I’m trying to test a list of 2000 payloads on my client and I thought I could do that somehow with mitmproxy.
So I wrote a script (pardon my python):

from mitmproxy import ctx
import argparse
import os
import json
import codecs


def start():
    parser = argparse.ArgumentParser()
    parser.add_argument("dir", type=str)
    args = parser.parse_args()

    ctx.index = 0
    ctx.data = []
    file_names = os.listdir(args.dir)
    for file_name in file_names:
         ctx.data.append(args.dir+file_name)


def response(flow):
    if flow.request.pretty_url.startswith("https://some_url"):
        with open(ctx.data[ctx.index], "r") as file:
            if len(ctx.data)-1 > ctx.index:
                flow.response.status_code = 200
                flow.response.content = file.read().encode('UTF-8')
                ctx.index += 1
            else:
                flow.response.status_code = 500

So it would essentially return a different json body on each request.
That works well but I don’t want to hammer my server at the same time.
Would there be a way to do that rewrite before the request starts?
Something like a replace like a -replace-from-file with a different body every time?

Thanks!

Hi,

Would https://github.com/mitmproxy/mitmproxy/blob/master/examples/simple/send_reply_from_proxy.py (switch to your version branch on GitHub!) work for you?

That’s exactly what I was looking for, thank you!