django rest framework XML Parse - 无效令牌

时间:2016-02-26 02:55:14

标签: python xml django rest django-rest-framework

我的最终目标是使用DRF将外部网站POST数据发送到我的网站。他们发送的数据是XML格式。

  • python 2.7
  • djangorestframework 3.3.2
  • djangorestframework-xml 1.3.0

我的问题是,只要外部网站发布数据,就会返回HTTP 400错误。但我可以把CURL的数据弄好。我无法找到我可以看到的位置或转储其余框架POST中的数据。我已经尝试将parser_context字典发送到CustomXMLParse Parse。但是我总是返回None,即使成功的CURL

也是如此

我覆盖了默认的XMLParser,因此我可以检索属性。我会说我能够在CURL时成功使用API​​。

成功卷曲

curl  http:/website/post/ -H "Content-Type: text/xml" -d "<registrationreport format='summary' regid='abc123' instanceid='0'><complete>complete</complete><success>failed</success><totaltime>19</totaltime><score>0</score></registrationreport>"

Views.py

@api_view(['POST'])
@authentication_classes((BasicAuthentication,))
@parser_classes((CustomXMLParser, ))
@permission_classes((IsAuthenticated,))
def coursePostBack(request):
      ...
      #Error from parsers.py STOPS before running anything in View

Parsers.py

class CustomXMLParser(XMLParser):
"""
Custom XML parser.
"""
logger.debug(' coursePostBack | PARSER  HIT ')

media_type = 'text/xml'

def parse(self, stream, media_type=None, parser_context=None):
    logger.debug(' coursePostBack | PARSER--PARSE HIT')
    assert etree, 'XMLParser requires defusedxml to be installed'

    parser_context = parser_context or {}

    encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET)
    parser = etree.DefusedXMLParser(encoding=encoding)
    try:
        ## This is where it fails! How can I SEE the data being sent in POST     ##
        tree = etree.parse(stream, parser=parser, forbid_dtd=True)
    except (etree.ParseError, ValueError) as exc:
        logger.error('XML Parse error - %s' % six.text_type(exc))
        raise ParseError('XML parse error - %s' % six.text_type(exc))
    data = self._xml_convert(tree.getroot())
    attribs = tree.getroot().attrib
    data['regid'] = attribs['regid']
    return data

Log.py

ERROR [training:36] XML Parse error - not well-formed (invalid token): line 1, column 1

0 个答案:

没有答案