Sending XML format request using HTTP POST method to send data from ServiceNow

Pradip1
Tera Contributor

I am working on an integration in which i need to send request from ServiceNow in XML format as per predefined schema shared with us.

I am using Outbound Rest Message -> POST.

I am facing two issues:

1. How to create an hierarchical XML from a glide record

2. I believe there is some encoding is required for posting this XML to third party API. 

Can someone help me?

1 ACCEPTED SOLUTION

ChrisBurks
Mega Sage

You should be able to do this in a couple of ways.
1) You can create the XML structure manually and use "${your variable name here}"  for values
2) Place that into the Content field under the HTTP Query parameters in the POST record
3) Use the "Auto-generate variables" to create a list of variables based on whatever you supplied in your xml structure
4) Then when you use the script to kick off the API call just pull from the correlating fields of the record and place in the script
Examples:
Screenshot of Rest POST form:

find_real_file.png

Screenshot of auto generated variable:
find_real_file.png

Script usage:

// Script in Business Rule
try { 
 var r = new sn_ws.RESTMessageV2('Name of your REST Message', 'POST');
 // take the current short description value from record and assign to correlatind 
 // variable in REST message
 r.setStringParameterNoEscape('shotdesc', current.short_description);

 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
}
catch(ex) {
 var message = ex.getMessage();
}

 

OR another way is use the script to generate the xml and set the request body via script

Example script:

// Scripted in Business Rule
try { 
 var r = new sn_ws.RESTMessageV2('Name of your REST Message', 'POST');

 // output of this array will just be a string representation of the xml structure
 var requestBodyXML = [
              '<request>',
               '<entry>',
              '<short_description>' + current.short_description + '</short_description>',
              '</entry>',
              '</request>'
 ].join("");

 r.setRequestBody(requestBodyXML);
 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();

}
catch(ex) {
 var message = ex.getMessage();
}

 

These are just basic examples. Of course the script can be adjusted to make it a little more dynamic with placing the values

 

View solution in original post

1 REPLY 1

ChrisBurks
Mega Sage

You should be able to do this in a couple of ways.
1) You can create the XML structure manually and use "${your variable name here}"  for values
2) Place that into the Content field under the HTTP Query parameters in the POST record
3) Use the "Auto-generate variables" to create a list of variables based on whatever you supplied in your xml structure
4) Then when you use the script to kick off the API call just pull from the correlating fields of the record and place in the script
Examples:
Screenshot of Rest POST form:

find_real_file.png

Screenshot of auto generated variable:
find_real_file.png

Script usage:

// Script in Business Rule
try { 
 var r = new sn_ws.RESTMessageV2('Name of your REST Message', 'POST');
 // take the current short description value from record and assign to correlatind 
 // variable in REST message
 r.setStringParameterNoEscape('shotdesc', current.short_description);

 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
}
catch(ex) {
 var message = ex.getMessage();
}

 

OR another way is use the script to generate the xml and set the request body via script

Example script:

// Scripted in Business Rule
try { 
 var r = new sn_ws.RESTMessageV2('Name of your REST Message', 'POST');

 // output of this array will just be a string representation of the xml structure
 var requestBodyXML = [
              '<request>',
               '<entry>',
              '<short_description>' + current.short_description + '</short_description>',
              '</entry>',
              '</request>'
 ].join("");

 r.setRequestBody(requestBodyXML);
 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();

}
catch(ex) {
 var message = ex.getMessage();
}

 

These are just basic examples. Of course the script can be adjusted to make it a little more dynamic with placing the values