about summary refs log tree commit diff
path: root/gn_libs/requests.py
blob: 18404aa37bf975591276ecba2aafca5a7f11c0a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
"""Utilities to deal with requests."""
from json.decoder import JSONDecodeError
from flask import request

def request_json() -> dict:
    """Retrieve the JSON sent in a request."""
    if "application/json" in request.headers.get("Content-Type", ""):
        try:
            return request.json or {}
        except JSONDecodeError:
            return {}

    if "multipart/form-data" in request.headers.get("Content-Type", ""):
        return dict(request.form) or {}

    return dict(request.args) or {}