Can we support a custom XML payload Inbound to a scripted REST API (XML with no request or entry tags)?

SueF
Kilo Contributor

Working on an integration with a client that can only POST XML over HTTP,  they can not consume web services SOAP or REST.   Before we go through the process of creating a scripted REST API, I want to know if this is even possible.  The XML inbound from the client does not have a SOAP envelope or the required <request><entry> tags for REST XML Posts.

Example XML looks something like this, with multiple hierarchical structures and without the REST <request> root tag or subsequent <entry> tag.

 

<incident [some proprietary namespace]>

        <taggroup1>

                  <field1/>

                  <field2/>

                  <field3/>

      </taggroup1>

        <taggroup2>

                  <field1>

                  <subgroup1>

                           <field1/>

                           <field2/>

                  </subgroup1>

            </taggroup2>

 <incident>

 

8 REPLIES 8

SueF
Kilo Contributor

Thanks for the replies, let me clarify a few things.  

The client can only send an XML Payload through POST (they can not support Json format or send HTTP GET).  Also, they do not have anything like the ServiceNow REST API Explorer, they use SOAP UI for testing the HTTP Post of the XML.  

They are not sending the xml wrappers: request/entry/body like you would get while using the ServiceNow REST API Explorer.  

Also, the XML example above is fictitious and used only to describe the type of XML payload and proprietary namespace they are sending, and to show the lack of root /request/entry/body tags that seemingly are required by ServiceNow REST APIs.  The example is in no way complete, accurate, or a representation of the payload being sent. 

If you try to use an external Tool to POST over HTTP (like SOAP UI) you will see the type of payload being sent that resembles what this client sending.   Can this be parsed by a scripted REST API?

Sorry that I didn't explain too well.

ServiceNow REST API Explorer is just a tool that can be used to test ServiceNow REST APIs. It's not a tool that's meant for actual usage in production. After testing the API, it's able to generate scripts and codes in curl, python, ruby, javascript, perl, and powershell that may actually be used in the production system.

For example, following is a curl script generated by REST API Explorer. (need to replace user name/password "admin" with actual user name and password).

curl "https://dev69219.service-now.com/api/522800/consume_xml_payload" \
--request POST \
--header "Accept:application/json" \
--header "Content-Type:application/xml" \
--data "<incident><taggroup1><field1>field1 string</field1><field2/><field3/></taggroup1><taggroup2><field1/><subgroup1><field1/><field2/></subgroup1></taggroup2></incident></body></entry></request>" \
--user 'admin':'admin'

Scripted REST Resource

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var returnValue = '';
    try {
        var xmlString = request.body.dataString;
        xmlDoc = new XMLDocument2();
        xmlDoc.parseXML(xmlString);
        returnValue = xmlDoc.getNodeText("/incident/taggroup1/field1");
    } catch (e) {
        returnValue = 'Unable to process xml document';
    }
    return {
        "requestString": returnValue
    };

})(request, response);

Execution result. I'm returning a value of xml document as a JSON in the example but this is just for example.

find_real_file.png

As shown above, it's possible to POST an XML message without request/entry and process it within the Scripted REST API.

Thank you very much for taking the time to show this and explain it.  We can then continue to pursue this approach.

ascdev
Kilo Explorer

I have a similar requirement to ingest XML data from POST requests that are of a format specified by the 3rd party service and do not satisfy service now's requirement of <request><entry> formatting.

It appears that when you have inbound JSON requests there's no special requirement of format.  Why is XML different and can we work around it?

 

EDIT: I just saw @Hitoshi Ozawa reply to the previous comment.  That worked for me.