How to configure Query job API in service now

How to configure Query job API in service now

Steps :
  1. Submit query job with POST /queryJobs , get the job id in response
  2. Poll query job with GET /queryJobs/{job_id} , if status received is “FINISHED“, go to next step.
  3. Get query job results with GET /queryjobs/${job_id}/results.

Add A REST Message for query job and add the 3 required apis to it :

        

POST query job and get query job id :


  1.         var postData = '{"query":' + query + ', "type":"global"}';
  2.         var message = new sn_ws.RESTMessageV2('Uptycs Query Job', 'POST Query Job');
  3.         message.setStringParameterNoEscape('url', this.url);
  4.         message.setStringParameterNoEscape('customer_id', this.customerId);
  5.         now = new Date();
  6.         message.setRequestHeader('date', now.toUTCString());
  7. message.setRequestHeader('Authorization', authToken;
  8.         message.setRequestHeader('content-type', 'application/json');
  9.         message.setRequestBody(postData);
  10.         var resp = msg.execute();
  11.         var respObj = JSON.parse(resp.getBody());
  12.         var queryJobId = respObj.id;

        

Poll Query job :

  1. createJobInfoMessage: function(queryJobId) {
  2.         var message = new sn_ws.RESTMessageV2('Uptycs Query Job', 'GET Query Job Info');
  3.         message.setStringParameterNoEscape('url', this.url);
  4.         message.setStringParameterNoEscape('customer_id', this.customerId);
  5.         message.setStringParameterNoEscape('job_id', queryJobId);
  6.         message.setRequestHeader('content-type', 'application/json');
  7.         now = new Date();
  8.         message.setRequestHeader('date', now.toUTCString());
  9. message.setRequestHeader('Authorization', authtoken;
  10.         return message;
  11.     },

  12.     pollQueryJob: function(queryJobId) {
  13.         var message = this.createJobInfoMessage(queryJobId);
  14.         var status = 'QUEUED';
  15.         do {
  16.             response = message.execute();
  17. var httpResponseStatus = response.getStatusCode();
  18.             responseObj = JSON.parse(response.getBody());
  19. gs.info('query job status code ' + queryJobId + ' ' + httpResponseStatus);
  20.             status = responseObj.status;
  21. // Check if status is undefined and exit the loop
  22. if (!status || httpResponseStatus != 200) {
  23. gs.error('status is undefined. Exiting the loop.');
  24. break;
  25. }
  26. if (status != 'FINISHED' && status != 'ERROR' && status != 'QUEUED' && status != 'RUNNING') {
  27.                 gs.error('status is not FINISHED, ERROR, RUNNING, or QUEUED.  status is: ' + status);
  28. break;
  29.             }
  30.             comp.wait(5000);
  31.         }
  32.         while (status != 'FINISHED' && status != 'ERROR');

  33.         if (status != 'FINISHED') {
  34.             this.missedSections.push(section);
  35.             gs.error('error: ' + responseObj.error.message.detail);
  36.         }
  37.         var nRows = responseObj.rowCount;
  38.         gs.warn(' nRows recieved: ' + nRows);
  39. return nRows;
  40. },

        

Get query job result :

each call to get results returns 10000 rows, use offset to pull all rows

  1.         var message = new sn_ws.RESTMessageV2('Uptycs Query Job', 'GET Query Jobs Results'); 
  2.         message.setStringParameterNoEscape('url', this.url);
  3.         message.setStringParameterNoEscape('customer_id', this.customerId);
  4.         message.setStringParameterNoEscape('job_id', queryJobId);
  5.         message.setStringParameterNoEscape('offset', offset);
  6.         now = new Date();
  7.         message.setRequestHeader('date', now.toUTCString());
  8.         message.setRequestHeader('content-type', 'application/json');
  9. message.setRequestHeader('Authorization',authToken);
  10.         response = msg.execute();
  11.         responseBody = response.getBody();
  12.         responseObj = JSON.parse(responseBody);
  13.         for (i = 0; i < responseObj.items.length; i++) { 
  14.             // loop over query result rows
  15.         }
        

    • Related Articles

    • API calling via Swagger

      Go To the "Account Settings". Click the "CREATE" button to generate API keys, which will be downloaded in JSON format. The downloaded JSON will contain information such as customerId, API key, API Secret, Domain, DomainSuffix, and other relevant ...
    • Provisioning roles using API

      Overview Starting with Uptycs portal release 46025, Uptycs provides entity level granular user permissions through roles. This document outlines the process to create roles using API - with examples. Procedure -- Create a new role $ urestapi -k ...
    • Create Custom Threat Source using API

      The article includes the API query to create and update the threat source. We support uploading CSV as of now. To create Threat source: curl --location --request POST ...
    • How to check Audit logs under upi_api_audit_logs

      The article contains useful queries about how to use the upt_api_audit_logs to capture audit log events. The api_name column in the table suggests how the api call was made. the following are the different values that can be present: Api call Query ...
    • Generate JWT Token using PowerShell CMDLETS

      With just PowerShell Cmdlets, you can generate a Bearer Auth Token using the following steps. # please install JWT module in PowerShell Install-Module JWT # cmdlet to generate JWT Token New-Jwt -Header '{"alg": "HS256", "typ": "JWT"}' -PayloadJson ...