Search code examples
jsonresponsemitmproxy

Modify JSON response body with mitmproxy


My response

{
    "Data": {
        "response": {
            "encrypt": "",
            "message": "0006",
            "status": "1",
            "unlock": null
        }
    },
    "ErrorCode": 0,
    "Message": null
}

I need modify "encrypt","message","status","unlock"

With this script these parameters are not modified but cloned & added under " "Message": null "

I use this code:

from mitmproxy import ctx
from mitmproxy import http
import json

def response(flow: http.HTTPFlow) -> None:
    if flow.request.pretty_url.endswith("/api/url/go"):
        data = json.loads(flow.response.get_text())
        data["encrypt"] = "5656"
        data["message"] = ""
        data["status"] = "0"
        data["unlock"] = ""        
        flow.response.text = json.dumps(data)

Solution

  • Based on the JSON blob you posted, it looks like you want to modify data["Data"]["response"]["encrypt"] and not data["encrypt"].