[英]Unable to use CURL within GROOVY script for a REST PUT call
我試圖使用CURL做一個簡單的PUT請求。 很簡單,它在終端上但無法在我的Groovy腳本中運行。
這是它的片段: -
class Test {
//Throws 415 Cannot Consume Content Type
void testPUT () {
println "curl -i -X PUT -H \"Content-Type: application/json\" -d '{\"Key1\":1, \"Key2\":\"Value2\"}' http://<hostname>/foo/".execute().text
}
// Works Perfectly Fine
void testGET () {
println "curl -i -X GET -H \"Content-Type: application/json\" http://<hostname>/foo".execute().text
}
}
我還嘗試使用三重引號括起命令,如: -
"""curl -i -X PUT -H "Content-Type:application/json" -d '{"Key1":1,"Key2":"Value2"}' http://<hostname>/foo""".execute().text
我所有的嘗試只是給出了415內容類型無法消費
當我在終端窗口上使用curl命令時,PUT和GET方法都可以正常工作。
我錯過了什么嗎? 任何幫助,將不勝感激!
謝謝!
嘗試使用字符串的列表變體,看看是否有效:
println ["curl", "-i", "-X PUT", "-H 'Content-Type:application/json'", "-d '{\"Key1\":1, \"Key2\":\"Value2\"}'", "http://<hostname>/foo/"].execute().text
我遇到了類似的問題,這是我找到解決問題的唯一方法。 Groovy會將字符串拆分為每個空格的參數,我懷疑這會使Curl和-H參數對絆倒。 通過將字符串放入列表變體中,它將每個項目保持在一起作為參數。
在Bruce的答案的基礎上,你還需要標記“-X PUT”。 在groovy 2.3.6上測試過。 ["curl", "-H", "Content-Type: application/json", "-H", "Accept: application/json", "-X", "PUT", "-d", data, uri].execute()
感謝xynthy列表變異提示,我仍然看到可怕的“內容類型'應用程序/ x-www-form-urlencoded'不支持”與您的示例,但分解-H和內容類型字符串工作。
這在groovy 1.8中得到了證實:
["curl", "-H", "Content-Type: application/json", "-H", "Accept: application/json", "-X PUT", "-d", data, uri].execute().text
首先我安裝了groovy post build插件
https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin
然后我在我的jenkins作業的后期構建配置中包含了groovy post build插件
並使用了該命令
"curl --request POST http://172.16.100.101:1337/jenkins/build".execute().text
這里我的端點是http:172.16.100.101:1337 / jenkins / build
這適用於我的終端
groovy -e "println 'curl -i -H \'Content-Type:application/json\' -XPUT -d \'{\"test\":4}\' http://google.fr/'.execute().text"
如果它不適合你,那么這可能不是一個常規問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.