通过代理从ServiceNow自定义JavaScript连接器执行POST

时间:2018-09-13 00:48:05

标签: javascript rest http-proxy servicenow servicenow-rest-api

我正在构建一个自定义JavaScript连接器,以从SaaS监视平台的API中读取数据,该数据将被馈送到ServiceNow事件管理模块中。我需要同时执行G​​ET和POST(对于oauth)。这要求JS在公司网络中位于代理后面的MID服务器上运行。 ServiceNow环境正在金斯敦上运行。

我已经根据周围的各种其他代码段(使用占位符API)编写了以下代码段,但是找不到任何可以指定代理服务器/端口以便相应地引导流量的内容。结果,连接超时。

var result = "";

try {
    // Instantiate the HTTPRequest object
    ms.log("JS Connector - Building request");
    var request = new HTTPRequest("https://jsonplaceholder.typicode.com/posts");

    // Add the headers and set http timeout
    ms.log("JS Connector - Adding headers");
    request.addHeader('Content-Type','application/json');
    request.setTimeout(5000);

    // Define the JSON payload
    var json_payload = JSON.parse('{"name":"blah","id":"123"}');
    ms.log("JS Connector - Payload: " + JSON.stringify(json_payload));

    // Go!
    ms.log("JS Connector - About to post...");
    var response = request.post(json_payload);

    // Output the results, if any
    if (response == null) {
        result = "The response returned was null";
        ms.log("JS Connector - " + result);
        this.addError(request.getErrorMessage());
    } else {
        var body = this.getResponseBody(response);
        ms.log("JS Connector - Return code " + response.getStatusCode());
        ms.log("JS Connector - Response body: " + JSON.stringify(body));
        return body;
    }
} catch(err) {
    this.addError("JS Connector - Failed to send data - Error: " + err.message);
    this.addError("JS Connector - Failed to send data - Status: " + httpStatus.toString());
    return "Error";
}

MID服务器代理日志的结果如下:

09/13/18 10:32:51 (846) Worker-Interactive:ConnectorProbe Slow execution (797ms) of script: script_include:Connector_JS
09/13/18 10:32:51 (846) Worker-Interactive:ConnectorProbe *** Script: Connector_JS - testing connection
09/13/18 10:32:51 (846) Worker-Interactive:ConnectorProbe *** Script: JS Connector - Building request
09/13/18 10:32:51 (846) Worker-Interactive:ConnectorProbe *** Script: JS Connector - Adding headers
09/13/18 10:32:51 (971) Worker-Interactive:ConnectorProbe *** Script: JS Connector - Payload: {"name":"blah","id":"123"}
09/13/18 10:32:51 (971) Worker-Interactive:ConnectorProbe *** Script: JS Connector - About to post...
09/13/18 10:33:02 (003) Worker-Interactive:ConnectorProbe WARNING *** WARNING *** java.net.SocketTimeoutException: connect timed out when posting to https://jsonplaceholder.typicode.com/posts
09/13/18 10:33:02 (003) Worker-Interactive:ConnectorProbe *** Script: JS Connector - The response returned was null
09/13/18 10:33:02 (003) Worker-Interactive:ConnectorProbe *** Script: java.net.SocketTimeoutException: connect timed out when posting to https://jsonplaceholder.typicode.com/posts

任何帮助将不胜感激。 谢谢堆。

0 个答案:

没有答案