我正在尝试使用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方法都能正常工作。
我错过了什么吗?任何帮助将不胜感激!
谢谢!
答案 0 :(得分:7)
尝试使用字符串的列表变体,看看是否有效:
println ["curl", "-i", "-X PUT", "-H 'Content-Type:application/json'", "-d '{\"Key1\":1, \"Key2\":\"Value2\"}'", "http://<hostname>/foo/"].execute().text
我遇到了类似的问题,这是我找到解决问题的唯一方法。 Groovy会将字符串拆分为每个空格的参数,我怀疑这会使Curl和-H参数对绊倒。通过将字符串放入列表变体中,它将每个项目保持在一起作为参数。
答案 1 :(得分:3)
在Bruce的回答基础上,您还需要标记化&#34; -X PUT&#34;。在groovy 2.3.6上测试过。 ["curl", "-H", "Content-Type: application/json", "-H", "Accept: application/json", "-X", "PUT", "-d", data, uri].execute()
答案 2 :(得分:0)
这适用于我的终端
groovy -e "println 'curl -i -H \'Content-Type:application/json\' -XPUT -d \'{\"test\":4}\' http://google.fr/'.execute().text"
如果它对您不起作用,那么这可能不是一个常规问题。
答案 3 :(得分:0)
感谢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
答案 4 :(得分:0)
首先我安装了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