Parsing Request Body in Scripted REST APIs — Avoid NullPointerException
A company uses ServiceNow to manage IT assets. An integration sends asset data from an external CMDB to ServiceNow via REST API. The external system sends a payload that includes the asset's serial number, location, and status. The developer has created a Scripted REST API to receive this data. However, when testing, the external system receives an HTTP 500 error. The developer inspects the script log and sees the error: 'java.lang.NullPointerException' at line 10 of the script. The script uses the GlideRecord API to query for an existing asset by serial number. The relevant code is: var gr = new GlideRecord('alm_asset'); gr.get('serial_number', request.body.serial); What is the most likely cause of the NullPointerException?
Quick Answer
The correct diagnosis works backward from the error itself: a NullPointerException means the script tried to read a property off something that does not exist, and here that something is request.body. In a Scripted REST API, request.body is only populated when ServiceNow successfully parses the incoming payload into an object, and that parsing depends on the request arriving with the right Content-Type header, typically application/json, or the resource being configured to accept a body at all. If either of those is missing, request.body stays null, and the moment the script tries to reach into it with request.body.serial, it is really trying to read .serial off null, which is exactly what throws the exception. The fix in a scenario like this is to confirm the caller is sending the correct Content-Type and that the Scripted REST resource is set up to expect and parse a body, rather than assuming the payload arrives as a ready-made object automatically. This pattern shows up constantly in integration troubleshooting: whenever an error trace points to a null reference immediately after code that expects parsed request data, the underlying cause is almost always that the data never got parsed in the first place, so the next step is always to check how the request was sent and how the endpoint is configured to receive it before assuming the logic further down the script is at fault.
⚠ Common exam trap
A common mix-up: candidates assume the NullPointerException is due to a field name typo or incorrect GlideRecord usage, but the real issue is that `request.body` is null because the request body was not parsed, which is a subtle but critical detail about REST API request handling in ServiceNow.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
The request body is not being parsed; 'request.body' is null because the content type is not set to JSON or the API is not expecting a body
The NullPointerException occurs when trying to access a property of a null object. In this case, `request.body` is null, meaning the request body was not parsed into a JavaScript object. This typically happens when the Content-Type header is not set to `application/json` or the Scripted REST API endpoint is not configured to expect a request body, so `request.body.serial` evaluates to `null.serial`, causing the exception.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
The 'serial_number' field in the 'alm_asset' table does not exist or is misspelled
Why it's wrong here
A missing field would cause an error when trying to set a value, not during get().
- ✗
The GlideRecord.get() method is incorrectly used; it should be gr.get('serial_number') without the value
Why it's wrong here
The correct syntax is gr.get('field', value); this is valid.
- ✓
The request body is not being parsed; 'request.body' is null because the content type is not set to JSON or the API is not expecting a body
Why this is correct
If the API does not parse the body, request.body remains null, causing a NullPointerException when accessing .serial.
- ✗
The external system is sending the serial number as part of the URL instead of the request body
Why it's wrong here
That would cause request.body to be empty, but the error would be different (e.g., undefined).
Go deeper
Related to this question
About these practice questions
This SNOW-CAD question is part of Courseiva's 481-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
Same concept, more angles
1 more way this is tested on SNOW-CAD
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. A developer is creating a REST API endpoint in ServiceNow that must accept JSON payloads and return a response. Which method should be used to parse the incoming request body?
easy- A.JSON.parse(request.body)
Why : In ServiceNow scripted REST APIs, the incoming request body is available as a string via `request.body`. To parse JSON payloads, you must use `JSON.parse(request.body)` to convert the string into a JavaScript object. Option B (`request.body.data`) is not a standard property; the request body is a single string. Option C (`dataString`) is not a property of the request object. Option D (`bodyJSON`) is not a method or property; it does not exist in the ServiceNow REST API context.
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This SNOW-CAD practice question is part of Courseiva's free ServiceNow certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the SNOW-CAD exam.