我有麻烦了。我需要向CMDB系统发送get请求,并将初始配置(如端点URI、路径、DB连接字符串等)检索为JSON对象。CMDB系统将返回一个JSON对象。如何在空手道-config.js中实现这一点。我应该编写自定义的javascript函数,还是有内置的功能?我检查了空手道对象部分,但是,我不知道如何做。
为此,我编写了几个定制的js函数。它们在我的系统中独立工作(我已经在我的机器上安装了node.js ),但不会在空手道-config.js中工作。
First one
var HttpClient = function () {
this.get = function (aUrl, aCallback) {
var XMLHttpRequest = require('xhr2');
var anHttpRequest = new XMLHttpRequest();
anHttpRequest.onreadystatechange = function () {
if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200)
aCallback(anHttpRequest.responseText);
}
anHttpRequest.open("GET", aUrl, true);
anHttpRequest.send(null);
}
}
var url = 'https://reqres.in/api/users/2';
var client = new HttpClient();
client.get(url, function (response) {
var response1 = JSON.parse(response);
console.log(response1)
});第二
var axios = require('axios');
// Make a request for a user with a given ID
axios.get('https://reqres.in/api/users/2').then(function (response) {
console.log(response)
}).catch(function (error) {
console.log(error);
});发布于 2018-03-28 02:45:55
我认为您忘记了空手道恰好非常擅长于发出HTTP请求:)
您所需要做的就是编写一个可重用的空手道特性文件,使GET到https://reqres.in/api/users/2。您可能需要找出所需的任何标头。
有一个karate.callSingle()方法,现在可以在karate-config.js中使用它来做您想做的事情。请看第31行:
https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/karate-config.js#L31
https://stackoverflow.com/questions/49522222
复制相似问题