无法在 GROOVY 脚本中使用 CURL 进行 REST PUT 调用
我正在尝试使用 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 Content Type Cannot be Consumed
当我仅使用curl命令时在终端窗口中,PUT 和 GET 方法都可以正常工作。
我错过了什么吗?任何帮助将不胜感激!
谢谢你!
I am trying to do a simple PUT request using CURL. Simple it is on a terminal but not able to get it working within my Groovy script.
Here is a snippet of it :-
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
}
}
I also tried to enclose the command using triple quotes like:-
"""curl -i -X PUT -H "Content-Type:application/json" -d '{"Key1":1,"Key2":"Value2"}' http://<hostname>/foo""".execute().text
All my attempts just gives 415 Content Type Cannot be Consumed
When I simply use the curl command on a terminal window, both PUT and GET methods work fine.
Am I missing something? Any help would be appreciated!
Thank You!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试使用字符串的列表变体,看看是否有效:
我遇到了类似的问题,这是我能找到解决它的唯一方法。 Groovy 会将字符串拆分为每个空格处的参数,我怀疑这会导致 Curl 和 -H 参数对出错。通过将字符串放入列表变体中,它将每个项目作为参数保存在一起。
Try using the list variation of the string and see if that works:
I was having a similar problem and this was the only way I could find to solve it. Groovy will split the string into arguments at each space and I suspect this was tripping up Curl and the -H argument pair. By putting the string into the list variant, it keeps each item together as an argument.
根据 Bruce 的答案,您还需要标记“-X PUT”。在 groovy 2.3.6 上进行了测试。
[“curl”,“-H”,“内容类型:application/json”,“-H”,“接受:application/json”,“-X”,“PUT”,“-d”,数据,uri].execute()
Building on Bruce's answer, you will also need to tokenize "-X PUT". Tested out on groovy 2.3.6.
["curl", "-H", "Content-Type: application/json", "-H", "Accept: application/json", "-X", "PUT", "-d", data, uri].execute()
这在我的终端中有效
如果它对您不起作用,那么这可能不是一个常规问题。
This works in my terminal
If it does not work for you, then this is likely not a groovy problem.
感谢 xynthy 提供的列表变化提示,我仍然看到可怕的
“不支持内容类型‘application/x-www-form-urlencoded’”
以您的示例为例,但是分解 -H 和内容类型字符串是有效的。
这已在 groovy 1.8 中得到证实:
Thanks xynthy for the list variation hint, I was still seeing the dreaded
"Content type 'application/x-www-form-urlencoded' not supported"
with your example, but breaking up the -H and the content type strings worked.
This is confirmed working in groovy 1.8:
首先,我安装了 groovy 后期构建插件
https://wiki.jenkins-ci .org/display/JENKINS/Groovy+Postbuild+Plugin
然后我在 jenkins 作业的后期构建配置中包含了 groovy 后期构建插件
,并使用了命令
Here我的端点是 http:172.16.100.101:1337/jenkins/build
First I installed the groovy post build plugin
https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin
Then I have included groovy post build plugin in my post build configuration of my jenkins job
and used the command
Here my endpoint is http:172.16.100.101:1337/jenkins/build