"Empty Response Thrown from REST API" when Attempting to Return JSON String
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2025 08:12 PM
Need assistance trying to pass the variable "c_num" from portal to a REST API as a query parameter.
The goal is to create a ServiceNow Script Include that:
- Receives a student ID (c_num) from a client-side form via GlideAjax.
- Fetches student data by calling an external REST API (LARF Banner Student Info) using the provided c_num as a query parameter.
- Processes the REST API response (which is a JSON object containing student information).
- Returns the student data (in JSON format) back to the client-side script.
However, I am getting the following error:
**Empty response received from REST API for c_num: C########
I figure this has something to do with the way the Script Include is trying to pass the c_num variable as the query parameter. What I don't know is if I should include ${c_num} as part of the base REST URL endpoint or leave it blank and let the Script Include handle everything.
var LARFStudentInformation = Class.create();
LARFStudentInformation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
// Client callable function to fetch student data
fetchStudentInfo: function() {
var cnum = this.getParameter('c_num'); // Get the c_num from the client
try {
gs.info('Script Include entered with c_num: ' + cnum);
if (!cnum) {
gs.error('Error: c_num is required');
return null;
}
// Create REST message object
var restMessage = new sn_ws.RESTMessageV2();
// Set the HTTP method to GET
restMessage.setHttpMethod('GET');
// Set the endpoint URL with variable substitution
restMessage.setEndpoint('.../gpa_hold_check/${c_num}'); // Use ${c_num} for the variable substitution
// Pass cnum as a query parameter (if required by the API)
restMessage.setQueryParameter('cnum', cnum); // REST API expects 'cnum' as the query parameter name
// Execute REST API call
var response = restMessage.execute();
var responseBody = response.getBody();
gs.info('API Response Body: ' + responseBody);
if (!responseBody) {
gs.error('Error: Empty response received');
return null;
}
// Parse the response
var parsedResponse = JSON.parse(responseBody);
if (parsedResponse.length === 0) {
gs.error('No student data found');
return null;
}
// Return the student data
return JSON.stringify(parsedResponse[0]);
} catch (e) {
gs.error('Error fetching student data for c_num ' + cnum + ': ' + e.message);
return null;
}
}
});
Not sure what goes in the REST in terms of c_num (i.e. the endpoint URL and http Query Parameters tab) vs. the Script Include? And why not call the REST by name in the SI instead of the using the full URL?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2025 09:49 PM
2 ways
1) update as this and don't use variable substituion
restMessage.setEndpoint('.../gpa_hold_check/' + c_num);
OR
2) use variable substitution and use setStringParameter() to set it
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2025 06:20 AM - edited 03-12-2025 06:42 AM
Thank you for the reply! Unfortunately, both methods return Error: Received non-successful HTTP status code: 0, suggesting the REST API call is not even reaching the server or the response is being blocked in some way. I can successfully return a JSON string when I use variable substitution to test the REST, but no response trying to pass the c_num field as an http parameter client-side.
Here are my settings for the REST config:
I am using the mid-server for authentication on the Default GET.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2025 05:15 PM
i have this example @appstorm on how you can dynamically pass a payload from the client side to the server side api. it provides good coding practices like OOB, SOLID principles, repository pattern and service oriented architecture. establishing reliability and traceability to your code. giving you full visibility using a global logger in every step of your script.
Dynamic Script Include and GlideAjax in ServiceNow: Scalable & Reusable Code for Architects