我正在使用{
问题是当我尝试定义自定义解析器(按照here)时,下面显示了一个说明问题的示例:
import logging
import json
import jsonschema
from rest_framework.exceptions import ParseError
from rest_framework.parsers import JSONParser
log = logging.getLogger('logger')
class JSONCustomParser(JSONParser):
def parse(self, stream, media_type=None, parser_context=None):
data = super(JSONCustomParser, self). \
parse(stream, media_type, parser_context)
try:
# these are executed normally
a = 1
b = 2
# problematic bits: un-comment either of these and execution ends early.
# data = json.load(stream.read())
# data = json.load("{}")
# data = JSONParser().parse(stream)
# auth_name = str(parser_context['request'].auth.application)
# sample commands not executed if they are *after* the above and either if them is
# un-commented
a = a + a
b = a + 2
except ValueError:
log.error("Value error")
except Exception as generic_exception:
log.error(str(generic_exception))
else:
return data
还有一个非常简单的测试视图,您可以使用:
class TestView(APIView):
"""
Simple test view to test the parser
"""
parser_classes = (JSONCustomParser,)
permission_classes = ()
def post(self, request):
content_length = request.META['CONTENT_LENGTH']
return Response({'received': content_length})
我假设两个stream
消费操作强制返回,尽管不太确定,因为流在super
调用中被JSONParser
消费。。。然而,我不确定为什么其他人会提出一个问题
目前没有回答
相关问题 更多 >
编程相关推荐