4

发送到特定 URL ( http://test.com) 的 POST 请求如下所示:

{
    "messageType": "OK",
    "city": {
        "Name": "Paris",
        "Views": {
            "1231": {
                "id": 4234,
                 "enableView": false
            },
        },
        "Views": [5447, 8457],
        "messages": [{
            "id": "message_6443",
            "eTag": 756754338
        }]
    },
    "client": {
        "Id": 53,
        "email": "test@test.us",
        "firstName": "test",
        "lastName": "test",
        "id": 52352352,
        "uuid": "5631f-grdeh4",
        "isAdmin": false
    }
}

我需要拦截请求并更改isAdmin为true。

对某个 URL ( https://test.com/profiles/{Random_Numbers}/{id}) 的 GET 请求具有如下(编码)响应:

{
    "id": 0, 
    "Code": "Admin", 
    "display": "RRRR"
}

我需要改成id5。

所以基本上我需要编写一个脚本来执行这两项操作。

到目前为止,我已经尝试利用 GitHub 上的一些示例,但到目前为止我还没有得到它:

from libmproxy.protocol.http import decoded

def start(context, argv):
  if len(argv) != 3:
   raise ValueError('Usage: -s "modify-response-body.py old new"')
    context.old, context.new = argv[1], argv[2]


def response(context, flow):
    with decoded(flow.response):  # automatically decode gzipped responses.
      flow.response.content = flow.response.content.replace(context.old, context.new)`

我如何为我的场景实现这个?

也许使用 libmproxy 来获取 http-request 和 response 可能是一个更好的主意。

4

1 回答 1

10

您发布的脚本和 Python 的 JSON 模块应该能让您走得更远:

def response(context, flow):
    if flow.request.url == "...": # optionally filter based on some criteria...
        with decoded(flow.response):  # automatically decode gzipped responses.
            data = json.loads(flow.response.content)
            data["foo"] = "bar"
            flow.response.content = json.dumps(data)
于 2015-04-28T08:43:43.420 回答