def require_json(func):
    """
    Decorator to require that a request has a JSON-encoded body.
    """
    @wraps(func)
    def wrapped(request, *args, **kwargs):
        """
        Try to JSON decode the body and
        save it on the HttpRequest object.
        In case it's not JSON, return a 400.
        """
        try:
            # we don't wanna raise for empty bodies though
            request.json = json.loads(request.body or '{}')
        except (__HOLE__, TypeError):
            return JsonResponse(data={'error': 'JSON body required.'}, status=400)
        return func(request, *args, **kwargs)
    return wrapped
dataset/ETHPy150Open m-vdb/github-buildservice-boilerplate/buildservice/utils/decorators.py/require_json