The Zurich release has arrived! Interested in new features and functionalities? Click here for more

REST XML API Response

andreaLuongo
Tera Expert

Hello,

I have a POST API. I send an XML via POST, and it is read correctly, and I can access the data. My problem occurs when I have to respond via XML. The error does not occur if I respond via JSON. If I use JSON as the response, the record is created correctly, and the message is returned.

(function process(request, response) {



    try {
        var requestString = request.body.dataString;
        var xmlDoc = new XMLDocument2();
        xmlDoc.parseXML(requestString);

        // Recupero i valori dei campi dal nodo TicketNotify
        var routingID = xmlDoc.getNodeText('/TicketNotify/routingID');
        var sessionID = xmlDoc.getNodeText('/TicketNotify/sessionID');
        var operationID = xmlDoc.getNodeText('/TicketNotify/operationID');
        var operationDetail = xmlDoc.getNodeText('/TicketNotify/operationDetail');
        var operationTimestamp = xmlDoc.getNodeText('/TicketNotify/operationTimestamp');
        var sourceTicketID = xmlDoc.getNodeText('/TicketNotify/sourceTicketID');
        var description = xmlDoc.getNodeText('/TicketNotify/description');
        var priority = xmlDoc.getNodeText('/TicketNotify/priority');

        // Creazione Work Order (tabella wm_order come esempio)
        var wo = new GlideRecord('wm_order');
        wo.addQuery('number', woNumber); // cerca per numero
        wo.query();

        var sysId;

        if (wo.next()) {
            wo.short_description = description;
            wo.priority = priority;
            sysId = wo.update();
        } else {
            wo.initialize();
            wo.short_description = description;
            wo.priority = priority;
            sysId = wo.insert();
        }

        var responseXML =
            '<?xml version="1.0" encoding="UTF-8"?>' +
            '<TicketNotify xmlns:xsi="http://**********" xmlns:xsd="http://***********">' +
            '<routingID xmlns="http://*********">' + routingID + '</routingID>' +
            '<sessionID xmlns="http://***********">' + sessionID + '</sessionID>' +
            '<requestedOperation xmlns="http://***********">' +
            '<operationID xmlns="http://**********">' + operationID + '</operationID>' +
            '<operationDetail xmlns="http://*************">' + operationDetail + '</operationDetail>' +
            '<operationTimestamp xmlns="http://***************">' + operationTimestamp + '</operationTimestamp>' +
            '</requestedOperation>' +
            '<destinationTicketID xmlns="http://*********************">' + sysId + '</destinationTicketID>' +
            '<sourceTicketID xmlns="http://****************">' + sourceTicketID + '</sourceTicketID>' +
            '<message xmlns="http://******************">' +
            '<code xmlns="http://****************">0</code>' +
            '<desc xmlns="http://***************">OK</desc>' +
            '</message>' +
            '</TicketNotify>';

        response.setStatus(200);
        response.setHeader("Content-Type", "application/xml; charset=UTF-8");
        var writer = response.getStreamWriter();
        writer.write(responseXML);
        writer.flush();

    } catch (ex) {
        var errorXML =
            '<TicketResponse>' +
            '<status>500</status>' +
            '<message>Errore: ' + ex.message + '</message>' +
            '</TicketResponse>';

        response.setStatus(500);
        response.setHeader('Content-Type', 'application/xml');
        response.getStreamWriter().write(errorXML);
    }
})(request, response);

andreaLuongo_0-1758792465999.png

Thank you in advance for your time.

3 REPLIES 3

Nawal Singh
Mega Guru

Hi @andreaLuongo,

Make sure request headers include:-

Content-Type: application/xml
Accept: application/xml

 

If you found my response helpful, please mark it as helpful and accept it as the solution.

Thank you
Nawal Singh

Bhuvan
Kilo Patron

@andreaLuongo 

 

Follow below article to use POST method via XML. Make sure you are passing the right header information and using XMLHelper class to convert XML to JavaScript objects for processing.

 

https://www.servicenow.com/community/in-other-news/scripted-rest-apis-part-2/ba-p/2294147

 

If this helped to answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan

Ankur Bawiskar
Tera Patron
Tera Patron

@andreaLuongo 

so is that a scripted REST API?

You should form response as JSON object and then convert it to XML string and then send

Scripted REST APIs - Part 2 

try this and see if it works

 

(function process(request, response) {

    try {
        var requestString = request.body.dataString;
        var xmlDoc = new XMLDocument2();
        xmlDoc.parseXML(requestString);
        var writer = response.getStreamWriter();

        // Read values from TicketNotify node
        var routingID = xmlDoc.getNodeText('/TicketNotify/routingID');
        var sessionID = xmlDoc.getNodeText('/TicketNotify/sessionID');
        var operationID = xmlDoc.getNodeText('/TicketNotify/operationID');
        var operationDetail = xmlDoc.getNodeText('/TicketNotify/operationDetail');
        var operationTimestamp = xmlDoc.getNodeText('/TicketNotify/operationTimestamp');
        var sourceTicketID = xmlDoc.getNodeText('/TicketNotify/sourceTicketID');
        var description = xmlDoc.getNodeText('/TicketNotify/description');
        var priority = xmlDoc.getNodeText('/TicketNotify/priority');

        // Work Order creation (wm_order table example)
        var wo = new GlideRecord('wm_order');
        wo.addQuery('number', woNumber); // Ensure woNumber is defined appropriately
        wo.query();

        var sysId;
        if (wo.next()) {
            wo.short_description = description;
            wo.priority = priority;
            sysId = wo.update();
        } else {
            wo.initialize();
            wo.short_description = description;
            wo.priority = priority;
            sysId = wo.insert();
        }

        var responseXML =
            '<?xml version="1.0" encoding="UTF-8"?>' +
            '<TicketNotify xmlns:xsi="http://**********" xmlns:xsd="http://***********">' +
            '<routingID xmlns="http://*********">' + routingID + '</routingID>' +
            '<sessionID xmlns="http://***********">' + sessionID + '</sessionID>' +
            '<requestedOperation xmlns="http://***********">' +
            '<operationID xmlns="http://**********">' + operationID + '</operationID>' +
            '<operationDetail xmlns="http://*************">' + operationDetail + '</operationDetail>' +
            '<operationTimestamp xmlns="http://***************">' + operationTimestamp + '</operationTimestamp>' +
            '</requestedOperation>' +
            '<destinationTicketID xmlns="http://*********************">' + sysId + '</destinationTicketID>' +
            '<sourceTicketID xmlns="http://****************">' + sourceTicketID + '</sourceTicketID>' +
            '<message xmlns="http://******************">' +
            '<code xmlns="http://****************">0</code>' +
            '<desc xmlns="http://***************">OK</desc>' +
            '</message>' +
            '</TicketNotify>';

        response.setStatus(200);
        response.setHeader("Content-Type", "application/xml; charset=UTF-8");
        writer.writeString(responseXML);

    } catch (ex) {
        var errorXML =
            '<?xml version="1.0" encoding="UTF-8"?>' +
            '<TicketResponse>' +
            '<status>500</status>' +
            '<message>Errore: ' + ex.message + '</message>' +
            '</TicketResponse>';

        response.setStatus(500);
        response.setHeader('Content-Type', 'application/xml; charset=UTF-8');
        writer.writeString(errorXML);
    }
})(request, response);

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader