将 curl 请求添加到 serviceNow 业务规则中 (javascript)
Adding a curl request into a serviceNow business rule (javascript)
简而言之:
我如何编写 javascript 代码来向我的 Flask 服务器发送 API PUT 消息?
上下文
我有一个简单的烧瓶应用程序,如下所述:http://flask-restful.readthedocs.io/en/0.3.5/quickstart.html
但我通过将此添加到顶部来添加了 CORS 功能:
from flask import Flask
from flask_restful import reqparse, abort, Api, Resource
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
api = Api(app)
它位于 [server_ip]
的服务器上
有一个名为 serviceNOW 的票务服务应用程序,它们允许在其业务规则中使用 运行 简单的 javascript 命令。
他们提供了上下文:
(function executeRule(current, previous /*null when async*/) {
})(current, previous);
我想添加功能:curl http://[server_ip]:5000/todos/todo3 -d "task=something different" -X PUT -v
主要想法:我想在调用业务规则时将 API 调用从 serviceNOW 服务器发送到我的 Flask 服务器
在 serviceNOW 服务器页面上使用 chrome 控制台,我有 运行 以下代码:
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "http://[my_server]:5000/todos/todo3", false);
xhttp.send();
我收到错误:
Mixed Content: The page at 'https://[serviceNOW instance]' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://[server_ip]:5000/todos/todo3'. This request has been blocked; the content must be served over HTTPS.
(anonymous) @ VM1629:1
VM1629:1 Uncaught DOMException: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://[server_ip]:5000/todos/todo3'.
at <anonymous>:1:7
编辑 1
我改了:
xhttp.open("GET", "http://[my_server]:5000/todos/todo3", false);
xhttp.send();
至
xhttp.open("PUT", "https://[my_server]:5000/todos/todo3", false);`
xhttp.send(JSON.stringify({ "task": "new task2"}));
我得到:
VM1698:1 OPTIONS https://[server_ip]:5000/todos/todo5
(anonymous) @ VM1698:1
VM1698:1 Uncaught DOMException: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'https://[server_ip]:5000/todos/todo5'.
at <anonymous>:1:7
我的服务器说:
code 400, message Bad HTTP/0.9 request type ('\x16\x03\x...more hex..')
?m?~)]?n??K...more stuf..?,?0̨̩????/5" 400 -
编辑 2
这是我当前的代码:
var xhttp = new XMLHttpRequest();
xhttp.open("PUT", "https://[my_server]:5000/todos/todo3", false);`
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.send(JSON.stringify({ "task": "new task2"}));
这是我当前的控制台端错误:
DOMException: Failed to execute 'send' on 'XMLHttpRequest': Failed to load
这是我当前的服务器端错误:
code 400, message Bad HTTP/0.9 request type
这个卷曲有效:
curl http://[server_ip]:5000/todos/todo5 -d "task=something differentyea" -X PUT -v
编辑 3
这是我当前的代码:
var xhttp = new XMLHttpRequest();
xhttp.open("PUT", "https://[my_server]:5000/todos/todo3", true);`
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.send(JSON.stringify({ "task": "new task2"}));
这是我当前的错误:
OPTIONS https://[server_ip:5000/todos/todo6 net::ERR_SSL_PROTOCOL_ERROR
编辑 4
此代码有效(注意 http 而不是 https):
var xhttp = new XMLHttpRequest();
xhttp.open("PUT", "http://[my_server]:5000/todos/todo3", true);`
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.send(JSON.stringify({ "task": "new task2"}));
但我必须从 none https 站点 运行 它。
ServiceNow 中的业务规则执行服务器端。在浏览器控制台中制作代码原型不会有帮助。最重要的是,XMLHttpRequest
在服务器端不存在。
要从业务规则进行 HTTP 调用,您必须使用 ServiceNow RESTMessageV2
API。有关使用信息和示例代码,请参阅 API Docs - RESTMessageV2。
您的代码可能如下所示:
var rm = new sn_ws.RESTMessageV2();
rm.setEndpoint('http://[my_server]:5000/todos/todo3');
rm.setHttpMethod('PUT');
rm.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
rm.setRequestBody(JSON.stringify({ "task": "new task2"}));
var response = rm.execute();
var responseBody = response.getBody();
// Process the response here...
response
是一个 RESTResponseV2 对象。有关与响应交互的更多信息,请参阅文档。
简而言之: 我如何编写 javascript 代码来向我的 Flask 服务器发送 API PUT 消息?
上下文
我有一个简单的烧瓶应用程序,如下所述:http://flask-restful.readthedocs.io/en/0.3.5/quickstart.html
但我通过将此添加到顶部来添加了 CORS 功能:
from flask import Flask
from flask_restful import reqparse, abort, Api, Resource
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
api = Api(app)
它位于 [server_ip]
有一个名为 serviceNOW 的票务服务应用程序,它们允许在其业务规则中使用 运行 简单的 javascript 命令。
他们提供了上下文:
(function executeRule(current, previous /*null when async*/) {
})(current, previous);
我想添加功能:curl http://[server_ip]:5000/todos/todo3 -d "task=something different" -X PUT -v
主要想法:我想在调用业务规则时将 API 调用从 serviceNOW 服务器发送到我的 Flask 服务器
在 serviceNOW 服务器页面上使用 chrome 控制台,我有 运行 以下代码:
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "http://[my_server]:5000/todos/todo3", false);
xhttp.send();
我收到错误:
Mixed Content: The page at 'https://[serviceNOW instance]' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://[server_ip]:5000/todos/todo3'. This request has been blocked; the content must be served over HTTPS.
(anonymous) @ VM1629:1
VM1629:1 Uncaught DOMException: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://[server_ip]:5000/todos/todo3'.
at <anonymous>:1:7
编辑 1
我改了:
xhttp.open("GET", "http://[my_server]:5000/todos/todo3", false);
xhttp.send();
至
xhttp.open("PUT", "https://[my_server]:5000/todos/todo3", false);`
xhttp.send(JSON.stringify({ "task": "new task2"}));
我得到:
VM1698:1 OPTIONS https://[server_ip]:5000/todos/todo5
(anonymous) @ VM1698:1
VM1698:1 Uncaught DOMException: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'https://[server_ip]:5000/todos/todo5'.
at <anonymous>:1:7
我的服务器说:
code 400, message Bad HTTP/0.9 request type ('\x16\x03\x...more hex..')
?m?~)]?n??K...more stuf..?,?0̨̩????/5" 400 -
编辑 2
这是我当前的代码:
var xhttp = new XMLHttpRequest();
xhttp.open("PUT", "https://[my_server]:5000/todos/todo3", false);`
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.send(JSON.stringify({ "task": "new task2"}));
这是我当前的控制台端错误:
DOMException: Failed to execute 'send' on 'XMLHttpRequest': Failed to load
这是我当前的服务器端错误:
code 400, message Bad HTTP/0.9 request type
这个卷曲有效:
curl http://[server_ip]:5000/todos/todo5 -d "task=something differentyea" -X PUT -v
编辑 3
这是我当前的代码:
var xhttp = new XMLHttpRequest();
xhttp.open("PUT", "https://[my_server]:5000/todos/todo3", true);`
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.send(JSON.stringify({ "task": "new task2"}));
这是我当前的错误:
OPTIONS https://[server_ip:5000/todos/todo6 net::ERR_SSL_PROTOCOL_ERROR
编辑 4
此代码有效(注意 http 而不是 https):
var xhttp = new XMLHttpRequest();
xhttp.open("PUT", "http://[my_server]:5000/todos/todo3", true);`
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.send(JSON.stringify({ "task": "new task2"}));
但我必须从 none https 站点 运行 它。
ServiceNow 中的业务规则执行服务器端。在浏览器控制台中制作代码原型不会有帮助。最重要的是,XMLHttpRequest
在服务器端不存在。
要从业务规则进行 HTTP 调用,您必须使用 ServiceNow RESTMessageV2
API。有关使用信息和示例代码,请参阅 API Docs - RESTMessageV2。
您的代码可能如下所示:
var rm = new sn_ws.RESTMessageV2();
rm.setEndpoint('http://[my_server]:5000/todos/todo3');
rm.setHttpMethod('PUT');
rm.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
rm.setRequestBody(JSON.stringify({ "task": "new task2"}));
var response = rm.execute();
var responseBody = response.getBody();
// Process the response here...
response
是一个 RESTResponseV2 对象。有关与响应交互的更多信息,请参阅文档。