summaryrefslogtreecommitdiffstats
path: root/api/parser.py
diff options
context:
space:
mode:
Diffstat (limited to 'api/parser.py')
-rw-r--r--api/parser.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/api/parser.py b/api/parser.py
new file mode 100644
index 0000000..63e283f
--- /dev/null
+++ b/api/parser.py
@@ -0,0 +1,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