Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify JSON response body with mitmproxy

When I send a request to a specific website API I get this as response

{
  "Tickets": [
    {
      "ticketAvailable": true,
...

How would I intercept and change ticketAvailable to false instead of true using mitmproxy?

from mitmproxy import ctx
from mitmproxy import http
import json

def response(flow: http.HTTPFlow) -> None:
    if flow.request.pretty_url == "https://example.com/api/v1/something":
        data = json.loads(flow.response.get_text())
        data["data"]["Tickets"]["ticketAvailable"] = false
        flow.response.text = json.dumps(data)

I have tried this, but it doesn't seem to work.

EDIT: Have not managed to fix it yet

from mitmproxy import ctx
from mitmproxy import http
import json

def response(flow: http.HTTPFlow) -> None:
    if flow.request.pretty_url == "https://example.com/api/v1/something":
        data = json.loads(flow.response.get_text())
        data["Tickets"][0]["ticketAvailable"] = "false"
        flow.response.text = json.dumps(data)
like image 789
riwejak558 Avatar asked Feb 28 '26 13:02

riwejak558


1 Answers

I figured out a way to fix it. I simply changed flow.request.pretty_url to flow.request.pretty_url.endswith

from mitmproxy import ctx
from mitmproxy import http
import json

def response(flow: http.HTTPFlow) -> None:
    if flow.request.pretty_url.endswith("/something"):
        data = json.loads(flow.response.get_text())
        data["Tickets"][0]["ticketAvailable"] = False
        flow.response.text = json.dumps(data)
like image 69
riwejak558 Avatar answered Mar 02 '26 03:03

riwejak558



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!