REST Example

 

When beginning an integration it is important to identify which capabilities are supported on both endpoints. In my case I was trying to integrate two systems which supported REST but not all HTTP Methods were supported by ServiceNow. ServiceNow supports the following Outbound REST Methods:

GET – Pull records

POST – Create records

PUT – Update records

DELETE – Delete records

Unfortunately for me in this case the remote system’s provided API only supported GET/POST/PATCH as a means to pull/create/update (PATCH usually used to partially update) records on the remote system and unfortunately sending REST data using the “PATCH” method is not currently supported in ServiceNow*. So what if you want to integrate with a 3rd party API that only works with PATCH to update data?

The answer, hope that the 3rd party provides an override method (most Microsoft products) that can be used with “POST” or “PUT”, both of which ARE supported in Outbound REST ServiceNow. In this scenario the 3rd party application has written special logic to handle incoming REST messages that are using one method (POST/PUT), but are checking for Request Headers to determine if the incoming data should be handled in another fashion (in our case PATCH).

The key to making this work (apart from confirming the 3rd party supports this) is in the HTTP Header of your REST Message you will need to set the Request Headers either in your REST Message or via code (depending on how you are implementing your REST functionality). Then setup the method to use POST or PUT so that ServiceNow knows how to send the REST message.

Below is an example of both ways in which we use the POST method to override in these examples; in this example the Request Headers needed were for ContentType and Override:

  • X-HTTP-Method-Override = “PATCH”
  • Content-Type  = “application/json-patch+json”

Here’s our example setting it up via a REST Message definition:

REST PATCH Method3

Or setting it up entirely in code:

var r = new sn_ws.RESTMessageV2();
r.setRequestHeader("Accept","application/json");
r.setRequestHeader('Content-Type','application/json-patch+json');
r.setRequestHeader('X-HTTP-Method-Override','PATCH');
r.setHttpMethod('post');
var url = 'https://integration.url.com';
r.setEndpoint(url);

*Note that PATCH is supported inbound to ServiceNow using REST, but does not support outbound PATCH (even though the REST API Explorer tool will generate non-working outbound PATCH code for you…) as of this post (in Helsinki).