2022-12-11 04:03:14 +00:00
|
|
|
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?
|
2022-12-27 23:18:00 +00:00
|
|
|
if request.content_type == "application/json" and request.body.strip():
|
2022-12-11 04:03:14 +00:00
|
|
|
return json.loads(request.body)
|
|
|
|
# Fall back to form data
|
|
|
|
value = {}
|
|
|
|
for key, item in request.POST.items():
|
|
|
|
value[key] = item
|
2023-01-28 17:05:28 +00:00
|
|
|
for key, item in request.GET.items():
|
|
|
|
value[key] = item
|
2022-12-11 04:03:14 +00:00
|
|
|
return value
|