【问题标题】:Modify json body with mitmproxy用 mitmproxy 修改 json body
【发布时间】:2021-06-17 11:48:35
【问题描述】:

我正在尝试拦截和修改 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 模拟器仍然得到原始响应。有谁知道如何将修改后的响应传递给设备?

【问题讨论】:

  • 你在这里所做的看起来不错,应该将修改后的响应传递给客户端。例如,你能用 cURL 重现这个吗?
  • iOS 模拟器是否使用了 mitmproxy(您是否看到来自 iOS 的请求?)并且您是否在模拟器中安装了 mitmproxy 根 CA 证书?
  • 是的。我可以看到设备的全部流量。一件奇怪的事情是,如果我将响应设置为空响应,我会在设备上得到它。

标签: python ios-simulator mitmproxy


【解决方案1】:

来自文档

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 事件挂钩

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多