Issue with create_request and JSON


As the title says, I’m having issues with create_request and JSON using mitmproxy 1.0.1

My code so far:

from mitmproxy import ctx
import json
def request(flow):
        data = json.loads(flow.response.text)
        f = ctx.master.create_request(method='GET', scheme='http', host='', path='/', port=80)
        flow.request.headers["Content-Type"] = "application/json"
        flow.request.content = json.dumps(['foo', {'bar': ('baz')}])
        ctx.master.replay_request(f, block=False)

I’m monitoring content sent via an Android app. I want to match a host from a request sent by the app and then send a created request with a bit of JSON. If the script is ran with flow.response.content, it errors and says JSON expects a string, not bytes. If I run the script on flow sent by the app, an empty request is created–the header and body are not changed. If I re-run the script on the created request, it fills out the header and body as expected.

Running the script at startup with the -s switch results in unique behavior. If the JSON references are commented out, the new request is created and filled out as expected. Leave the JSON references in, and it gives an error that NoneType object has no attribute ‘text’. What am I doing wrong?


You are trying to access to response in the request() hook - that likely won’t work. :slight_smile:


That did it. This may not mean much to you, but right now you’re my favorite person.