无法在 GROOVY 脚本中使用 CURL 进行 REST PUT 调用

发布于 2024-12-25 02:29:39 字数 845 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

黯然 2025-01-01 02:29:39

尝试使用字符串的列表变体,看看是否有效:

println ["curl", "-i", "-X PUT", "-H 'Content-Type:application/json'", "-d '{\"Key1\":1, \"Key2\":\"Value2\"}'", "http://<hostname>/foo/"].execute().text

我遇到了类似的问题,这是我能找到解决它的唯一方法。 Groovy 会将字符串拆分为每个空格处的参数,我怀疑这会导致 Curl 和 -H 参数对出错。通过将字符串放入列表变体中,它将每个项目作为参数保存在一起。

Try using the list variation of the string and see if that works:

println ["curl", "-i", "-X PUT", "-H 'Content-Type:application/json'", "-d '{\"Key1\":1, \"Key2\":\"Value2\"}'", "http://<hostname>/foo/"].execute().text

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.

耶耶耶 2025-01-01 02:29:39

根据 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()

独闯女儿国 2025-01-01 02:29:39

这在我的终端中有效

groovy -e "println 'curl -i -H \'Content-Type:application/json\' -XPUT -d \'{\"test\":4}\' http://google.fr/'.execute().text"

如果它对您不起作用,那么这可能不是一个常规问题。

This works in my terminal

groovy -e "println 'curl -i -H \'Content-Type:application/json\' -XPUT -d \'{\"test\":4}\' http://google.fr/'.execute().text"

If it does not work for you, then this is likely not a groovy problem.

愛放△進行李 2025-01-01 02:29:39

感谢 xynthy 提供的列表变化提示,我仍然看到可怕的
“不支持内容类型‘application/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

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:

["curl",  "-H", "Content-Type: application/json", "-H", "Accept: application/json", "-X PUT", "-d", data, uri].execute().text
天涯沦落人 2025-01-01 02:29:39

首先,我安装了 groovy 后期构建插件

https://wiki.jenkins-ci .org/display/JENKINS/Groovy+Postbuild+Plugin

然后我在 jenkins 作业的后期构建配置中包含了 groovy 后期构建插件

,并使用了命令

"curl --request POST http://172.16.100.101:1337/jenkins/build".execute().text 

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

"curl --request POST http://172.16.100.101:1337/jenkins/build".execute().text 

Here my endpoint is http:172.16.100.101:1337/jenkins/build

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文