CURL - NodeJS Rest API call Imperva - [SyntaxError: Unexpected token <]

CURL - NodeJS Rest API call Imperva - [SyntaxError: Unexpected token <]

我写了一个 shell 脚本代码,它在 Imperva system/instance.

中成功添加了一个新的 OS 连接 IP
## Create a new IP OS connection in a given site, server group.
create_ip()
{
 JSESSIONID=""
 addThisIP=""
 siteName=""
 serverGroupName=""
 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 会话的 logoff/out,这样我就不会达到限制)并在 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 <]

我正在寻找 2 个东西:
1.如何解决错误(如上图)
2. 如何在上面的 NodeJS 代码中传递 CURL 的 -d 选项 {...} 主体参数,就像我在 shell 脚本中使用的那样。

PS:将 POST 更改为 GET(方法),确保代码语法正确,因为它成功显示了 IP (使用 GET 操作)与上面的 NodeJS 代码。

所以,只有当我使用 POST 时(即当我尝试创建一个IP) -vs- GET(我用它来查找 IP 是否存在)。我根据 Imperva API 文档进行了检查,响应采用 JSON 格式。 不确定是否由于缺少 bullet# 2 问题,我在 POST 中遇到此语法错误。

好的。

第 1 条和第 2 条的解决方案是在 NodeJS 中设置 CURL“-d 选项值”,如下所示(我之前将其设置为空白):

  var data = '{"connection-mode":"SSH","host-name":"thisLinuxServer.fdqn","OS-type":"linux","user-name":"enter_your_userID"}'

现在,选项和数据都将发送到 httpHelp(options,data) 并且它会起作用。

以上只是一个虚拟数据值。通常我会在 JSON object.

中传递有效的 host-name

没想到不设置数据变量会导致这个语法错误。

另外,我错过了很多时间 headers 变量中 'Content-Type' 的值。这就是原因,错误消息中有“<”括号(因为默认情况下它会查找 <body>...</body> 部分包含一些 XML 格式 但在我们的例子中,HTML 响应包含或以 JSON { ... } 格式返回body 部分。这就是它显示 意外标记 <.

错误的原因

根据 API 文档,它应该是 "JSON" 类型,因此将该行更改为

'Content-Type': 'application/json',