"Empty Response Thrown from REST API" when Attempting to Return JSON String

appstorm
Tera Contributor

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:

  1. Receives a student ID (c_num) from a client-side form via GlideAjax.
  2. Fetches student data by calling an external REST API (LARF Banner Student Info) using the provided c_num as a query parameter.
  3. Processes the REST API response (which is a JSON object containing student information).
  4. 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?

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@appstorm 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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:

appstorm_3-1741786169542.png

 

appstorm_0-1741785893949.png

appstorm_1-1741785935364.png

appstorm_2-1741785976377.png

I am using the mid-server for authentication on the Default GET.

 

BillMartin
Mega Sage
Mega Sage

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