Dar*_*hak 6 rest groovy curl get 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
}
}
Run Code Online (Sandbox Code Playgroud)
我还尝试使用三重引号括起命令,如: -
"""curl -i -X PUT -H "Content-Type:application/json" -d '{"Key1":1,"Key2":"Value2"}' http://<hostname>/foo""".execute().text
Run Code Online (Sandbox Code Playgroud)
我所有的尝试只是给出了415内容类型无法消费
当我在终端窗口上使用curl命令时,PUT和GET方法都可以正常工作.
我错过了什么吗?任何帮助,将不胜感激!
谢谢!
小智 8
尝试使用字符串的列表变体,看看是否有效:
println ["curl", "-i", "-X PUT", "-H 'Content-Type:application/json'", "-d '{\"Key1\":1, \"Key2\":\"Value2\"}'", "http://<hostname>/foo/"].execute().text
Run Code Online (Sandbox Code Playgroud)
我遇到了类似的问题,这是我找到解决问题的唯一方法.Groovy会将字符串拆分为每个空格的参数,我怀疑这会使Curl和-H参数对绊倒.通过将字符串放入列表变体中,它将每个项目保持在一起作为参数.
基于布鲁斯的回答,您还需要标记“-X PUT”。在 groovy 2.3.6 上测试过。["curl", "-H", "Content-Type: application/json", "-H", "Accept: application/json", "-X", "PUT", "-d", data, uri].execute()
这适用于我的终端
groovy -e "println 'curl -i -H \'Content-Type:application/json\' -XPUT -d \'{\"test\":4}\' http://google.fr/'.execute().text"
Run Code Online (Sandbox Code Playgroud)
如果它对你不起作用,那么这可能不是一个常见的问题。