2016-05-10 77 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 
}