我编写了一个shell脚本代码,该代码在Imperva系统/实例中成功添加了新的OS连接IP。
## Create a new IP OS connection in a given site, server group.
create_ip()
{
JSESSIONID="${1}"
addThisIP="${2}"
siteName="${3}"
serverGroupName="${4}"
echo -e "\n- Trying to create a new OS connection IP now.\n";
##Make sure while initiating a REST API call, any parameter which has ' ' (space) in Imperva should be substituted with '%20'.
##JSESSIONID will be generated first and will be available to this function
create_new_ip_output="$(curl -ik -X POST -H "Cookie: JSESSIONID=$JSESSIONID" -H "Content-Type: application/json" -H "Accept: application/json" "https://${MX}:8083/SecureSphere/api/v1/conf/serverGroups/${siteName// /%20}/${serverGroupName// /%20}/servers/${addThisIP}" -d '{"connection-mode":"SSH","host-name":"thisLinuxServer.fdqn","OS-type":"linux","user-name":"enter_your_userID"}')";
return_code="$?";
if [[ "${return_code}" == "0" && ! `echo "${create_new_ip_output}" | grep "do not have permission"` ]]; then
echo -e "\n\n- OS connection IP (${addThisIP}) created successfully in site: ${siteName}, servergroup: ${serverGroupName} and stdout:\n${create_new_ip_output}\n";
return 0;
else
echo -e "\n\n- Failed to create a new OS connection IP (${addThisIP}) in site: ${siteName} and servergroup: ${serverGroupName}\n- using session ID: ${JSESSIONID}, error log:\n${create_new_ip_output}\n";
return 1;
fi
}
好的,上面的代码工作正常,一旦运行,我看到有效输出显示IP是否成功添加或失败消息是什么。
现在,我正在尝试在 NodeJS 中实现相同的功能。
为此,我已成功创建了生成JSESSIONID的函数(以便我可以使用它来执行多个REST / API调用操作,而不是每次为任何操作创建新会话),成功删除JSESSIONID(即注销/退出Imperva会话,以便我没有达到限制)并成功搜索Imperva系统中的IP(如果之前已添加)。
使用上面的shell脚本逻辑,我编写了以下NodeJS代码,使用Rest / API在Imperva中添加新的OS连接IP,但是我收到了一个错误。
//Imperva create IP
//qData is a hash array that has valid index/value pair values.
var impervaCreateIP = function(qData){
var deferred = Q.defer();
var data = ''
var options = {
hostname: 'myImpervaServerInstance.mycompanydomain.com',
port: 8083,
method: 'POST',
path: '/SecureSphere/api/v1/conf/serverGroups/'+ qData['siteName'] + '/' + qData['serverGroupName'] + '/servers/' + qData['ip'],
headers: {
'Content-Type': 'application/xml',
'X-Requested-With': 'Nodejs',
'Cookie': 'JSESSIONID='+qData['sid']
}
}
//svtLog() is a logger that I'm using to log messages to a file. Out of scope of this post.
svtLog('info','Imperva','Inside impervaCreateIP function')
svtLog('info','Imperva',qData['ip'])
svtLog('info','Imperva',qData['siteName'])
svtLog('info','Imperva',qData['serverGroupName'])
svtLog('info','Imperva','')
console.log('Inside impervaCreateIP')
console.log(options);
httpHelp(options, data)
.then(function(fullResponse){
var result = JSON.parse(fullResponse[1])
console.log("11 -----")
console.log(result)
console.log("22 -----")
deferred.resolve(qData['sid'])
console.log(result)
})
.fail(function(e){
svtLog('error','Imperva','Failed to add IP in Imperva')
svtLog('info','Imperva',qData['ip'])
svtLog('error','Imperva',e)
svtLog('info','Imperva','')
deferred.reject(e)
})
return deferred.promise;
}
错误讯息:
Inside impervaCreateIP
{ hostname: 'myImpervaServerInstance.mycompanydomain.com',
port: 8083,
method: 'POST',
path: '/SecureSphere/api/v1/conf/serverGroups/some%20Site%20NameInImperva/someServerGroupNameInImperva_01/servers/10.20.30.40',
headers:
{ 'Content-Type': 'application/xml',
'X-Requested-With': 'Nodejs',
Cookie: 'JSESSIONID=7B3C378D365B673F6C749847DEDC7D8F } }
[SyntaxError: Unexpected token <]
我正在寻找两件事:
1.如何解决错误(如上所示)
2.如何在上面的NodeJS代码中传递CURL的 -d选项{...} 正文参数,就像我在shell脚本中使用它一样。
PS :将POST更改为GET(方法),确保代码语法正确,因为它使用上述NodeJS代码成功显示IP(使用GET操作)。
所以,只有当我使用 POST 时(即当我尝试创建IP时)失败 -vs-GET(我用它来查找IP)是否存在)。我按照Imperva API文档进行了检查,响应采用JSON格式。 不确定是否由于缺少子弹#2问题,我在POST时遇到此语法错误。
答案 0 :(得分:0)
行。
子弹#1和#2的解决方案是设置CURL&#34; -d选项值&#34;在NodeJS中,如下所示(我之前将其设置为空白):
var data = '{"connection-mode":"SSH","host-name":"thisLinuxServer.fdqn","OS-type":"linux","user-name":"enter_your_userID"}'
现在,选项和数据都将发送到httpHelp(选项,数据),并且它可以正常工作。
以上只是一个虚拟数据值。通常我会在JSON对象中传递有效的主机名。
从未想过不设置数据变量会导致此语法错误。
其他的事情,我错过了大的时间是&#39; Content-Type&#39;内部头部变量。这就是原因,错误信息是&#34;&lt;&#34;支持(默认情况下,它会查找包含 XML格式的 <body>...</body>
部分,但在我们的示例中,包含或以JSON格式返回的HTML响应主体部分的 {...} 格式化方式。这就是意外令牌&lt; 显示错误的原因。
根据API文档,它应该是&#34; JSON&#34;类型,因此将该行更改为
'Content-Type': 'application/json',