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

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 = []
    file_names = os.listdir(args.dir)
    for file_name in file_names:

def response(flow):
    if flow.request.pretty_url.startswith("https://some_url"):
        with open([ctx.index], "r") as file:
            if len( > ctx.index:
                flow.response.status_code = 200
                flow.response.content ='UTF-8')
                ctx.index += 1
                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?



Would (switch to your version branch on GitHub!) work for you?

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