pravasi
pravasi

Reputation: 101

Django REST Framework - XMLRenderer modifying my simple XML response

I want to send a simple CXML text string as below as a response to a DRF POST request to a django view (see my django view below)

<?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:[email protected]" timestamp="2021-10-19T04:01:56.530426+00:00"><Response><Status 
code="200" text="Success" /></Response></cXML>

(Above string is value of 'response_cxml' variable in my django view below)

But what django sends and is received by the remote app is:

<?xml version="1.0" encoding="utf-8"?>
<root>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;!DOCTYPE cXML SYSTEM 
"http://xml.cXML.org/schemas/cXML/1.2.011/cXML.dtd"&gt;&lt;cXML payloadID="2021-10- 
19T05:10:[email protected]" timestamp="2021-10- 
19T05:10:41.115674+00:00"&gt;&lt;Response&gt;&lt;Status code="200" text="Success" 
/&gt;&lt;/Response&gt;&lt;/cXML&gt;</root>

XMLREnderer I believe is adding extra duplicate version tag and 'root' tag.It is also urlencoding xml characters like <,> etc.How can I prevent XMLREnderer modifying my response? The sender HTTP headers are Accept= "text/xml and content-type='text/xml'

My view:

@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'))

Upvotes: 3

Views: 624

Answers (1)

pravasi
pravasi

Reputation: 101

@renderer_classes([XMLRenderer]) - is not needed. Response() not needed.I used HTTPResponse and it went fine.

from django.http import HttpResponse

return HttpResponse(response_cxml,content_type="text/xml")

Upvotes: 4

Related Questions