How to send REST data using the “PATCH” Method from ServiceNow

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).

How to Parse JSON Data?

I was working with a client recently who was just starting their journey into the world of Web Services using REST. This is a very powerful technology which allows two systems to exchange data using HTTP [POST (Create), GET (Read), PUT (Update/Replace), PATCH (Update/Modify), DELETE (Delete)] with JSON (JavaScript Object Notation) to exchange data. To work with this JSON data, ServiceNow gives a few methods. The confusing part to most new admins is that there are separate methods depending on if the parsing is done on the Server side, or Client side. Here are the methods to use for both:

Client Side:

JSON.stringify(yourJSONObject) //Encode your object into a string
JSON.parse(yourJSONString) //Decode your JSON String into an Object

Server Side:

new global.JSON().encode(yourJSONObject); //Encode your object into a String
new global.JSON().decode(yourJSONString); //Decode your JSON String into an Object

One nice thing about using REST in ServiceNow is that they provide a number of APIs (Table API, Aggregate, Import Set, Performance Analytics) based upon what type of integration is needed.

An additional nice feature that came in the Fuji release was the “REST API Explorer” which allows you to easily test the different REST methods with different data.

A similar tool that can be used externally to test can be found at http://hurl.it

Here are some more links to help you along your REST journey:
http://wiki.servicenow.com/index.php?title=Getting_Started_with_REST#gsc.tab=0
http://wiki.servicenow.com/index.php?title=REST_API
http://wiki.servicenow.com/index.php?title=Outbound_REST_Web_Service#gsc.tab=0
http://wiki.servicenow.com/index.php?title=Table_API#gsc.tab=0
http://wiki.servicenow.com/index.php?title=REST_API_Explorer#gsc.tab=0
http://wiki.servicenow.com/index.php?title=Scripting_Outbound_REST#gsc.tab=0