向serviceNow业务规则(javascript)中添加curl请求
简而言之: 如何编写javascript代码,将API PUT消息发送到flask服务器 上下文 我有一个简单的烧瓶应用程序,如下所述: 但我在顶部添加了CORS功能:向serviceNow业务规则(javascript)中添加curl请求,javascript,python,curl,flask,servicenow,Javascript,Python,Curl,Flask,Servicenow,简而言之: 如何编写javascript代码,将API PUT消息发送到flask服务器 上下文 我有一个简单的烧瓶应用程序,如下所述: 但我在顶部添加了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) 它位于[se
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\u ip]
有一个名为serviceNOW的票务服务应用程序,它们允许用户在其业务规则中运行简单的javascript命令
它们提供了以下背景:
(function executeRule(current, previous /*null when async*/) {
})(current, previous);
我想添加以下功能:curl http://[server\u 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
到
我得到:
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
编辑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"}));
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"}));
这是我当前的控制台端错误:
DOMException: Failed to execute 'send' on 'XMLHttpRequest': Failed to load
code 400, message Bad HTTP/0.9 request type
这是我当前的服务器端错误:
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", false);`
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.send(JSON.stringify({ "task": "new task2"}));
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):
但是我必须从非https站点运行它。服务中的业务规则现在执行服务器端。在浏览器控制台中原型化代码将没有帮助。最重要的是,
XMLHttpRequest
服务器端不存在
要从业务规则进行HTTP调用,必须使用ServiceNowRESTMessageV2
API。有关用法信息和示例代码,请参阅
您的代码可能如下所示:
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...
响应
是一个对象。有关与响应交互的更多信息,请参阅文档