I am getting error while loading data through Data source Type REST (IntegrationHub)

DebraLord
Kilo Contributor

I have created Data source type: Rest(IntegrationHub) and Configured Request action to fetching data.
I have tested that request action in flow designer and I got below Response body:

{"offset":0,"fields":["CustomFields_network_hostname","Client_filewave_client_name","Client_device_name","Client_serial_number","DesktopClient_device_product_name","CustomFields_organization_unit","DesktopClient_last_logged_in_username","Client_last_ldap_username","DesktopClient_last_check_in","Client_current_ip_address"],"limit":1,"values":[   and so on.

 

but when i try to fetch that information through data source form link "Load All Records". so i am getting error

Message: javax.json.JsonException: Cannot auto-detect encoding, not enough chars

 

The issue is that I can't use JSON parser to fix this issue in flow designer because Data Source Request don't allow to add any step in flow designer.

 

Please provide any solution how can i fix this issue. I want to load data in import set table from response body .

 

Below, i have attached the some images. 

2 REPLIES 2

Shaqeel
Mega Sage

Hi @DebraLord 

 

Create a Scripted REST Data Source:

Create a new Data Source of type Scripted REST.

In the Script field of the Data Source, write a script that makes the REST call, parses the response, and handles the JSON properly.

(function processRESTResponse(source, input) {
    // Create a RESTMessageV2 object for calling the REST API
    var restMessage = new sn_ws.RESTMessageV2();
    
    // Set the endpoint and HTTP method (modify according to your actual API)
    restMessage.setHttpMethod('GET');
    restMessage.setEndpoint('https://api.yourendpoint.com/data');

    // Optionally set headers or query parameters if needed
    restMessage.setRequestHeader('Accept', 'application/json');
    
    // Execute the request
    var response = restMessage.execute();
    
    // Check for response errors
    if (response.getStatusCode() !== 200) {
        gs.error('Error: Unable to fetch data from the API. Status Code: ' + response.getStatusCode());
        return;
    }

    // Parse the response body
    var responseBody = response.getBody();
    var parsedData;
    
    try {
        // Parse JSON response body
        parsedData = JSON.parse(responseBody);
    } catch (e) {
        gs.error('Error: Failed to parse JSON response. ' + e.message);
        return;
    }

    
    var records = parsedData.values; // Or any specific field that contains the records
    if (!records || records.length === 0) {
        gs.info('No records found in the API response.');
        return;
    }

    for (var i = 0; i < records.length; i++) {
        var record = records[i];
        
        var importRecord = new GlideRecord('your_import_set_table');
        importRecord.initialize();

        importRecord.setValue('network_hostname', record.CustomFields_network_hostname);
        importRecord.setValue('client_name', record.Client_filewave_client_name);
        importRecord.setValue('device_name', record.Client_device_name);
        importRecord.setValue('serial_number', record.Client_serial_number);
        importRecord.setValue('organization_unit', record.CustomFields_organization_unit);
        importRecord.setValue('last_logged_in_username', record.DesktopClient_last_logged_in_username);
        importRecord.setValue('last_check_in', record.DesktopClient_last_check_in);
        importRecord.setValue('current_ip_address', record.Client_current_ip_address);

        
        importRecord.insert();
    }

    gs.info('Data loaded successfully into the Import Set table.');

})(source, input);

 

 

Regards

Shaqeel


***********************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

***********************************************************************************************************************





Regards

Shaqeel

Daniel Peel
Mega Sage

I can't speak to your specific situation, but if your Data Stream is in Global, build it in a scoped application and try again... I found this solved my issue with the "Cannot auto-detect encoding, not enough chars" error.