Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use CURL within GROOVY script for a REST PUT call

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!

like image 238
Darshak Avatar asked Jan 06 '12 03:01

Darshak


People also ask

How do you call cURL command in Groovy?

You could execute the curl command as a shell command using the "execute()" method available on groovy strings. Or you can use some native html processing classes. This will give you some native parsing of the response and response error handling etc.

How do you send a cURL request in API?

curl Syntax -X or --request - HTTP method to be used. -i or --include - Include the response headers. -d or --data - The data to be sent to the API. -H or --header - Any additional headers to be sent.

What is cURL in REST API?

'cURL' is a command-line tool that lets you transmit HTTP requests and receive responses from the command line or a shell script. It is available for Linux distributions, Mac OS X, and Windows. To use cURL to run your REST web API call, use the cURL command syntax to construct the command.


4 Answers

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.

like image 53
xynthy Avatar answered Oct 18 '22 08:10

xynthy


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

like image 39
user3240644 Avatar answered Oct 18 '22 08:10

user3240644


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.

like image 37
fixitagain Avatar answered Oct 18 '22 08:10

fixitagain


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
like image 33
Bruce Edge Avatar answered Oct 18 '22 08:10

Bruce Edge