使用mitmproxy修改json主体



我正在尝试拦截和修改graphql响应的主体。这是我的插件代码:

from mitmproxy import ctx
from mitmproxy import http
import json
def response(flow: http.HTTPFlow) -> None:
if flow.request.pretty_url == "https://my.graphql/endpoint":
request_data = json.loads(flow.request.get_text())
if request_data["operationName"] == "MyOperationName":
data = json.loads(flow.response.get_text())
data["data"]["product"]["name"] = "New Name"
flow.response.text = json.dumps(data)

我可以在mitmproxy控制台中看到修改后的响应。但我使用的iOS模拟器仍然得到了最初的响应。有人知道我如何将修改后的响应传递到设备吗?

来自文档

def response(self, flow: mitmproxy.http.HTTPFlow):
"""
The full HTTP response has been read.
Note: If response streaming is active, this event fires after the entire body has been streamed.
HTTP trailers, if present, have not been transmitted to the client yet and can still be modified.
"""
ctx.log(f"response: {flow=}")

看起来您可能正在流式传输响应体,这意味着修改将被忽略。

考虑使用def request事件挂钩而不是

最新更新