summaryrefslogtreecommitdiffstats
path: root/api/parser.py
blob: 63e283f1bdadb8d48c5ec05cd316d9dc5398a55b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import json

from ninja.parser import Parser


class FormOrJsonParser(Parser):
    """
    If there's form data in a request, makes it into a JSON dict.
    This is needed as the Mastodon API allows form data OR json body as input.
    """

    def parse_body(self, request):
        # Did they submit JSON?
        if request.content_type == "application/json":
            return json.loads(request.body)
        # Fall back to form data
        value = {}
        for key, item in request.POST.items():
            value[key] = item
        return value