Tuesday 22 July 2014

curl usage or how to post to a webservice from the commandline


curl is super for talking to http from the command line.

HTTP GET

In general, to use curl to do a GET request do as follows:

curl -v -H "header:value" http://host:port/webservice/uri

HTTP POST

To POST to a RESTful webservice, in this instance a JAX-RS Jersey REST service running on Jetty on an IBM AIX box:

  • Using curl 7.30.x from Windows
curl -H "header:value" -X POST -d "{json}" http://host:port/webservice/postResource
Note that you may have to escape the double quote in a json string, e.g. '{\"Key\":,\"Value\"}'  when posting the JSON data right on the command line.
  • Using curl 7.9.3 on the IBM AIX, the POST argument aint gonna fly:
curl -H "header:value" -X POST -d "{json}" http://host:port/webservice/postResource

Here's a tip - Passing the json on the command line is actually not great, its tough to read and tough to write out.

Rather create a file for the json packet and pass as an argument to curl like this:
curl -H "header:value" -X POST -d @myfilename.txt http://host:port/webservice/postResource

No comments:

Post a Comment