将curl请求添加到serviceNow业务规则(javascript)中

2024-10-03 04:27:46 发布

您现在位置:Python中文网/ 问答频道 /正文

简而言之: 如何编写一个javascript代码来向flask服务器发送apiput消息?在

上下文

我有一个简单的烧瓶应用程序,如下所述:http://flask-restful.readthedocs.io/en/0.3.5/quickstart.html

但我在顶部添加了以下功能:

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命令。在

它们提供了上下文:

^{pr2}$

我想添加功能: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"}));

但我必须从一个非https站点运行它。在


Tags: httpsipsendhttpflasknewtaskserver
1条回答
网友
1楼 · 发布于 2024-10-03 04:27:46

服务中的业务规则现在执行服务器端。在浏览器控制台中对代码进行原型化是没有帮助的。最重要的是,XMLHttpRequest不存在服务器端。在

要从业务规则进行HTTP调用,必须使用servicenawRESTMessageV2API。有关用法信息和示例代码,请参见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对象。有关与响应交互的更多信息,请参阅文档。在

相关问题 更多 >