Ejemplo n.º 1
0
    def process_request(self, request):
        # Abort early if we don't have any work to do
        params_json = request.body
        if not params_json:
            return

        # Reject unrecognized content types. Empty string indicates
        # the client did not explicitly set the header
        if request.content_type not in ('application/json', '', 'plain/text'):
            e = exception.ValidationError(attribute='application/json',
                                          target='Content-Type header')
            return render_exception(e, request=request)

        params_parsed = {}
        try:
            params_parsed = jsonutils.loads(params_json)
        except ValueError as ee:
            e = exception.ValidationError(attribute='valid JSON',
                                          target='request body')
            return render_exception(e, request=request)
        finally:
            if not params_parsed:
                params_parsed = {}

        params = {}
        for k, v in six.iteritems(params_parsed):
            if k in ('self', 'context'):
                continue
            if k.startswith('_'):
                continue
            params[k] = v

        request.environ[PARAMS_ENV] = params
Ejemplo n.º 2
0
 def _from_json(self, datastring):
     try:
         return jsonutils.loads(datastring)
     except ValueError:
         msg = _("cannot understand JSON")
         raise exception.MalformedRequestBody(reason=msg)