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

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

  1. Thanks , I’ve just been searching for information on this topic for a while and yours is the best I have discovered so far!

  2. What parameters we need to send, if we are trying to override “Patch” through “PUT” instead of “POST” ?
    Also want to clarify after setting these parameter from ServiceNow side in Outbound Rest, is there anything which need to be changed or modified at other tool side in there API for taking the “Patch” through “PUT” or “POST” .

    It’s very urgent please suggest as early as possible.

    Thanks in Advance !!!

    1. Hi Mohit,

      To send a PUT instead of a POST then it should be as simple as setting the HTTP method to PUT instead of POST:

       r.setHttpMethod('PUT'); 

      In terms of the “remote system” you will need to make sure that it supports sending the PATCH as an override to PUT (as in your question, or POST as in my example). This is the piece of code which indicates that the remote system’s API would need to be configured to look for within the PUT command in order to accept the “PATCH”:

       r.setRequestHeader('X-HTTP-Method-Override','PATCH') 

      If the remote system supports this it should be documented in their API.

      1. I want to send ” Patch” through ” PUT” then also I need to set r.setHttpMethod(‘PUT’) or I need to send r.setHttpMethod(‘Patch’).

        1. Mohit,

          Assuming you are in fact trying to “Override” your “PUT” REST Message with a “PATCH” command (since ServiceNow doesn’t allow the sending of PATCH commands), then you will want to follow my previous response/comment by using r.setHttpMethod(‘PUT’).

Leave a Reply

Your email address will not be published. Required fields are marked *