aboutsummaryrefslogtreecommitdiffstats
path: root/mitmproxy/contentviews/json.py
blob: 15c624add0ca528dab0292325e2921f9e52d878d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import json
from typing import Optional

from mitmproxy.contentviews import base


def pretty_json(s: bytes) -> Optional[bytes]:
    try:
        p = json.loads(s.decode('utf-8'))
    except ValueError:
        return None
    pretty = json.dumps(p, sort_keys=True, indent=4, ensure_ascii=False)
    return pretty.encode("utf8", "strict")


class ViewJSON(base.View):
    name = "JSON"
    content_types = [
        "application/json",
        "application/json-rpc",
        "application/vnd.api+json"
    ]

    def __call__(self, data, **metadata):
        pj = pretty_json(data)
        if pj:
            return "JSON", base.format_text(pj)