我想发送一个简单的CXML文本字符串,如下所示,作为对django视图的DRF POST请求的响应(参见下面的django视图)
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE cXML SYSTEM
"http://xml.cXML.org/schemas/cXML/1.2.011/cXML.dtd"><cXML payloadID="2021-10-
19T03:57:08.416995@example.com" timestamp="2021-10-19T04:01:56.530426+00:00"><Response><Status
code="200" text="Success" /></Response></cXML>
(上面的字符串是'response_cxml'变量在我的django视图的值)
但是django发送和远程应用接收的是:
<?xml version="1.0" encoding="utf-8"?>
<root><?xml version="1.0" encoding="UTF-8"?><!DOCTYPE cXML SYSTEM
"http://xml.cXML.org/schemas/cXML/1.2.011/cXML.dtd"><cXML payloadID="2021-10-
19T05:10:41.115609@example.com" timestamp="2021-10-
19T05:10:41.115674+00:00"><Response><Status code="200" text="Success"
/></Response></cXML></root>
XMLREnderer我相信是添加额外的重复版本标签和'根'标签。它还对诸如<,>等我怎么能防止XMLREnderer修改我的响应?发送方HTTP报头是Accept= "text/xml和content-type='text/xml'
我的观点:
@csrf_exempt
@api_view(['PUT','POST'])
@authentication_classes((TokenAuthentication,))
@permission_classes((IsAuthenticated,))
@renderer_classes([XMLRenderer])
@parser_classes([XMLParser])
def purchase_order(request):
"""
Receives the purchase order cXML and saves PO details to database.
Sends a 200 response back
"""
if request.method == 'PUT' or request.method == 'POST':
po_cxml = request.body.decode()
response_cxml = process_po_cxml(po_cxml)
return Response( response_cxml.encode('utf-8'))
@renderer_classes([XMLRenderer]) -不需要。不需要Response()。我使用了HTTPResponse,它运行良好。
django。导入http
返回HttpResponse (response_cxml content_type ="文本/xml")