2016-05-10 23 views
0

我需要帮助,找出为什么我的cURL查询的最后两个参数被忽略。使用groovy创建命令行字符串cURL - cURL忽略选项

请不要评论如何这不是做休息电话的最佳方式。我知道。这将是一种回退方法/解决另一个问题。 我manyl处理我的休息与wslite(1.1.2)API。

现在让我解释我在做什么: 我正在使用groovy shell执行程序通过cURL为其他服务创建命令行调用。

我建立了一个小类构建查询字符串和处理命令行:

class Curl { 

    def static getUserLogin(){ 
     def url     = '"https://some-login.someSystem-dev.someHost.com/someResource.beyond.foobar/login/LoginAUser ' 
     def requestFilePath  = '-d @temp/LoginPayload.json ' 
     def heads    = "-H 'Content-Type: application/json' -H 'Accept: text/plain' " 
     def params    = '-k -v' //-k = ignore unsecure -v = more verbose output 
     def fullurl    = url+requestFilePath+heads+params 
     return ex(fullurl) 
    } 

    /** 
    * 
    * @param _command The command you want to execute on your shell. 
    * @param _workingDir Optional: You may specify the directory where the command will be executed. Default is user dir. 
    * @return Exit value for the process. 0 = normal termination. 
    */ 
    def static ex(String _command, File _workingDir = new File(System.properties.'user.dir')) { 
    println "Executing command> $_command \n" 
    def process = new ProcessBuilder(addShellPrefix(_command)) 
             .directory(_workingDir) 
             .redirectErrorStream(true) 
             .start() 
    process.inputStream.eachLine {println it} 
    process.waitFor(); 
    return process.exitValue().value 
    } 

    private static addShellPrefix(String _command) { 
    def commandArray = new String[2] 
    commandArray[0] = "curl " 
    commandArray[1] = _command 
    return commandArray 
    } 
} 


Curl.getUserLogin() //to execute 

我希望的代码是自explenatory不够。这一切工作正常与简单的URL分别与较少的参数。

执行,这将产生以下的响应(摘自完整的调试输出):

执行命令> “https://some-login.someSystem-dev.someHost.com/someResource.beyond.foobar/login/LoginAUser” -d @温度/ LoginPayload.json -H“内容 - 类型:应用/ JSON” -H '接受:text/plain的' -k -v

%合计%收稿%Xferd平均速度时间时间时间 目前 DLOAD上传一共花左速度

0 0 0 0 0 0 0 0 - : - : - - : - : - - : - : - 0 0 0 0 0 0 0 0 0 - : - : - - - : - : - - - : - : - 0 curl:(60)SSL证书问题:证书链中的自签名证书更多详细信息,请参阅: http://curl.haxx.se/docs/sslcerts.html

curl默认执行SSL证书验证,使用 “证书颁发机构(CA)公钥”(CA证书)“捆绑”。如果 缺省包文件不合适,则可以使用--cacert选项指定备用文件 。如果此HTTPS服务器使用由该软件包中的CA签名的证书 ,则证书 验证可能由于证书 的问题而失败(可能已过期,或者该名称可能与 中的域名不匹配,则URL )。如果您想关闭卷曲验证 证书,请使用-k(或--insecure)选项。

现在,您可以看到我已将所需的选项“-k”附加到查询字符串,但不知何故它被忽略。直接在Windows命令行工具中使用此字符串(如果您尝试此操作,请确保您转义可能的双引号)尽管工作良好。

任何想法为什么会发生这种情况,或者我可以如何获得更多的调试信息?

Thx提前!

更新: 解决方案:
传递ever选项作为单个参数(通过列表)解决了问题。

新问题: 之后,我想使用'-o C:\ Temp \ response.txt'向参数列表输出对文件的响应。从命令行工具使用时,此工作正常。从Groovy脚本结果执行它:

卷曲:(23)失败书写体(!0 = 386)

我可以仅通过写流一个文件解决这个问题。真正困扰我的是这样一个事实,即答复似乎没有包含任何身体信息。按照预期,从Windows命令行工具执行curl命令会返回一个相当长的标记。

Andy的想法?

回答

1

如果您使用ProcessBuilder,则必须将每个参数作为自己的参数。你给构造函数的两个参数,程序名和剩下的参数作为一个参数,就好像你在命令行中围绕整个字符串引用引号一样。使fullurl成为一个列表,而每个参数都是它自己的列表元素,它应该按预期工作。你可以并且应该忽略任何其他的引用,就像你在URL中所做的那样。

+0

无论你和@Renato的answere都非常好。然而,你的说明我的实际问题。贡纳接受这个答案。 –

1

您的代码可以大大改善。您不应该将命令部分连接成单个字符串,只需使用List。

此外,变量的_前缀通常用于私有字段或内部函数,而不是明显不是内部函数的方法参数。

在Groovy中使用String数组很奇怪,你应该学习一些Groovy!

不管怎么说,这里有一个更好的版本代码:

def static getUserLogin() { 
    def url = '"https://some-login.someSystem-dev.someHost.com/someResource.beyond.foobar/login/LoginAUser' 
    def requestFilePath = '-d @temp/LoginPayload.json' 
    def heads = "-H 'Content-Type: application/json' -H 'Accept: text/plain' " 
    def insecure = '-k' 
    def verbose = '-v' 
    return ex([ url, requestFilePath, heads, insecure, verbose ]) 
} 

/** 
* 
* @param commands The command + args you want to execute on your shell. 
* @param _workingDir Optional: You may specify the directory where the command will be executed. Default is user dir. 
* @return Exit value for the process. 0 = normal termination. 
*/ 
static ex(List<String> commands, File _workingDir = new File(System.properties.'user.dir')) { 
    println "Executing command> $commands \n" 
    def process = new ProcessBuilder(addShellPrefix(commands)) 
      .directory(_workingDir) 
      .inheritIO() 
      .start() 
    process.waitFor() 
    return process.exitValue().value 
} 

private static addShellPrefix(List<String> commands) { 
    [ 'curl' ] + commands 
}