Search code examples
pythonmitmproxy

Issue Converting Older MITMProxy Scripts to work on 5.2 - Error on Replace and Can't Find Docs on What Replaced was Replaced with


I Updated to MITMProxy Version 5.2. After updating I tried to run an Older Script and Kept Getting the Error:

AttributeError: 'HTTPResponse' object has no attribute 'replace'

From my code:

flow.response.replace('FindThis', 'ReplaceWithThis')

I read that they Change Replace, but couldn't find any documents that were CLEAR on what they changed it to or the correct Syntax in a Script.

I read ModifyBody was a replacement, but when I changed my script from replace to ModifyBody, I would then get the error:

AttributeError: 'HTTPResponse' object has no attribute 'ModifyBody'

So I assume it is not correct or I need to load a module? And i would like to know if there is a new or same Syntax.

Thanks


Solution

  • We removed HTTPResponse.replace because it wasn't really clear which parts (headers, content) were replaced how. In practical terms:

    • Use flow.response.content = flow.response.content.replace(b"foo", b"bar") to make binary replacements in the response body.
    • Use flow.response.text = flow.response.text.replace("foo", "bar") to make text replacements in the response body.
    • Use flow.headers as a dictionary to make header replacements, e.g. flow.headers["foo"] = "42".