-1

https://instance.service-now.com/sys_report_template.do?CSV&jvar_report_id=xxxxx

此 URL 将仅下载提及 id 的报告而未提及 report_id 我如何获取所有报告?

4

1 回答 1

0

这是一些粗略的 nodejs 代码,但应该可以帮助您入门;

var https = require('https');
var username = '';
var password = '';
var url = 'dev32369.service-now.com';
var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64');
https.request({
  host: url,
  port: 443,
  path: '/api/now/table/sys_report?sysparm_query=field_listISNOTEMPTY&sysparm_fields=sys_id,title,filter,field,table,field_list&sysparm_limit=2',
  method: 'GET',
  headers: {
    'Authorization': auth,
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  }
}, function (reportResponse) {
  console.log('STATUS: ' + reportResponse.statusCode);
  // console.log('HEADERS: ' + JSON.stringify(res.headers));
  var reportResponseBody = '';
  reportResponse.setEncoding('utf8');
  reportResponse.on('data', function (chunk) {
    reportResponseBody += chunk;
    // console.log('BODY: ' + chunk);
  });
  reportResponse.on('end', function () {
    var reports = JSON.parse(reportResponseBody).result;
    console.log(reports);
    if (reports[0].field_list != '') {
      for (var x = 0; x < reports.length; x++) {
        var report = reports[x];
        var path = '';
        path += '/api/now/table/' + report.table;
        path += '?sysparm_fields=' + report.field_list;
        path += '&sysparm_query=' + encodeURIComponent(report.filter);
        path += '&sysparm_display_value=true';
        path += '&sysparm_exclude_reference_link=true';
        // path += '&sysparm_limit=2';
        console.log(path);
        var optionsCSV = {
          host: url,
          port: 443,
          path: path,
          method: 'GET',
          headers: {
            'Authorization': auth,
            'Accept': 'application/json',
            'Content-Type': 'application/json'
          }
        };
        console.log(optionsCSV);
        https.request(optionsCSV, function (csvResponse) {
          try {
            var csvResponseBody = '';
            csvResponse.on('data', function (chunk) {
              console.log('downloading report[' + report.title + ']...');
              csvResponseBody += chunk;
            });
            csvResponse.on('end', function () {
              //console.log(csvResponseBody);
              var csvResponseObj = JSON.parse(csvResponseBody).result;
              console.log(csvResponseObj);
              //this is a json obj but you can rewriet is csv here
            });
            csvResponse.on('error', function (err) {
              console.log(err);
            });
          } catch (e) {
            console.log(e);
          }
        }).end();
      }
    }
  });
}).end();
于 2018-07-20T22:25:08.637 回答