我的最终目标是使用DRF将外部网站POST数据发送到我的网站。他们发送的数据是XML格式。
我的问题是,只要外部网站发布数据,就会返回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>"
@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
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
ERROR [training:36] XML Parse error - not well-formed (invalid token): line 1, column 1