Have you ever run into unexpected behavior when making inbound REST calls to your ServiceNow instance? Perhaps the result of a GET doesn’t contain all of the records you expect it to, or nothing happens when you try to modify a record.
In this post, we’ll explore some of the options available for debugging inbound REST API calls and the Business Rules / ACLs that might be impacting those calls.
I’m going to introduce a few concepts that are not well known later in this post: The Log File Tailer and the X-WantDebugMessages & X-WantSessionNotificationMessages headers.
Handling Unexpected Results When Retrieving Records
Let’s start by looking at a common scenario we might encounter. We want to retrieve a list of Incident records using the Table API. However, when the results come back from the API call, we notice that there are missing records in the response and we can’t see every field that we expect.
So, what should we do next? With practice, you’ll start to get a better feel for when you should try one thing or another, but here’s the approach I generally take.
1. Validate assumptions
Before starting down the path of more complex debugging techniques, check the basics. Take a look at the records in a list or form view and make sure they are what you think they are. For this example, we want every incident where Category is Inquiry/Help. Do you see every incident in a list view that you expect to see?
Next, impersonate the user used to make API calls. In this example, the incident count went down to 18. Clearly something is filtering the results.
Enable Business Rule and Security Debugging
If you haven’t used the Debug Business Rule and Debug Security session debug options before, they’re tremendously useful tools when trying to determine how Business Rules and ACLs are impacting a transaction or query. To enable them, navigate to System Diagnostics > Session Debug. Click on Debug Business Rule and again on Debug Security.
With these debug options enabled, navigate to a list or form view and examine the output at the bottom of the screen. There’s a LOT of useful information here that can help us determine what’s happening during the transaction.
Here’s the Debug Business Rule output. We can see that there is a “before query” rule running before results are returned to us. Before query business rules have the potential to filter out records, so this is one of the first places to look if records are “missing”. In this example, we identified a business rule that is filtering out incidents opened by VIP users if the logged in user doesn’t have the incident_vip role. This explains why we can see the records as admin, but we cannot see the records from our integration user.
Taking a look at the Debug Security output, we find that an Access Control is preventing us from reading the value of the “secure_value” field.
At this point, we’ve identified the issue without doing anything special with the API call just by using standard platform debugging tools.B However, it isn’t always this simple. Throughout the rest of this post, we’ll look at ways to get more debug output that is specific to the API calls we’re making.
2. Validate the HTTP request and enable REST Debugging
Errors or unexpected results are often the result of small discrepancies in the HTTP call you’re making. Maybe there’s a bad query parameter or broken path in the URL. You can look for these sorts of issues by enabling REST Debugging and examining the node log.
- Create a new true/false system property named glide.rest.debug with value true.
- Initiate a test API call from a testing tool such as REST API Explorer, Postman, etc.
- Examine the node log file.
There’s just one big problem: we don’t want to download the node log every time we make a test request, and this is where the Log File Tailer comes in handy.
Using the Log File Tailer
The Log File Tailer allows you to view the node log for the node you’re currently connected to in real time. To use it, navigate to https://
REST debug log lines will start with something like this: 10/18/17 23:25:25 (984) 6825C50B137D8B409718B9B76144B0C1 #206480 [REST API]:
Use these lines to examine the details of the incoming request. You will see HTTP request headers, messages from the REST API code in the platform and more. A few words of warning about the Log File Tailer:
- Do not use in production
- Only keep the tailer window open for a short period of time
3. Session Debugging, Business Rule and Security Debugging
If everything looks good in the node log, it’s time to enable more types of debug output.
Enable Debugging for your Application
There are a number of debugging options available to you, but I’m going to focus on three. Session Debugging, Business Rule Debugging and Security Debugging.When you write gs.debug statements in your code, it doesn’t get displayed in the log file by default. If you want to see these debug messages, you must first enable session debugging. This increases the logging verbosity for the duration of your current session.
- Navigate to **System Applications > Application
- Click the Enable Session Debug button
Getting Debug Messages in the REST API response
One of the challenges with using the Node Log is that there can be a lot of other stuff there, and it can be difficult to determine which log entries are part of the request you’re debugging. Ideally, we want to see session debug message as part of the REST API response itself. This can be accomplished by sending one or both of the following HTTP headers with the API requests:
- X-WantSessionDebugMessages: true
- X-WantSessionNotificationMessages: true
To demonstrate how these work, I created a simple Scripted REST API with a method that writes debug messages.
Then, in REST API Explorer, I added the X-WantSessionDebugMessages header.
I then sent the request, and this is what the response looks like:
The X-WantSessionNotificationMessages Header
It’s common for Business Rules to use methods like gs.addInfoMessage(msg) or gs.addErrorMessage(msg) to present information to the current user. These messages are only displayed in the UI by default, but we can also see these messages in REST responses by adding the X-WantSessionNotificationMessages header. Here’s a Business Rule Script that writes both debug and info messages. It executes before query.
We trigger the rule by initiating a GET request. Similar to the previous example, we add the headers in REST API Explorer:
And in the response, we can see both messages!
Using External API Testing Tools
When you enable session debugging, as the name suggests, debugging is a enabled for your current session. This is a problem if you want to see debug messages from an API testing tool like Postman or Paw since the API request won’t be coming from your browser session. In order to get session debug messages from an external tool, you must also send your current JSESSIONID took and an additional X-UserToken header.
The JSESSIONID cookie
Use your browser’s developer tools to inspect the cookies for your current session. Here’s what this looks like in Google Chrome.
Open Chrome Developer Tools
Navigate to the Application tab and inspect your cookies
Copy the JSESSIONID cookie value
Set this aside for the next section.
The X-UserToken value
You must also send your current Session Token with the request in the X-UserToken header. The easiest way to get this token is to navigate to the ServiceNow UI in your browser and View Source. Look for this line:
This is your current session token. Copy the value in quotes and set it aside for the next step.
Put it all together
Now that you have:
- JSESSIONID token
- Session Token
- Enabled session (and other) debugging in ServiceNow
We can construct the request. Here’s what my example request looks like in Paw, the API testing tool I use.
Start with the basics. Use simple list/form views and impersonation before you dig into the more complicated options.
I covered Business Rule and Security Debugging, but there are other session debug options to explore. Check out the other options under the Session Debug menu for additional kinds of output.