How to test web service using command line curl How to test web service using command line curl curl curl

How to test web service using command line curl


Answering my own question.

curl -X GET --basic --user username:password \     https://www.example.com/mobile/resourcecurl -X DELETE --basic --user username:password \     https://www.example.com/mobile/resourcecurl -X PUT --basic --user username:password -d 'param1_name=param1_value' \     -d 'param2_name=param2_value' https://www.example.com/mobile/resource

POSTing a file and additional parameter

curl -X POST -F 'param_name=@/filepath/filename' \     -F 'extra_param_name=extra_param_value' --basic --user username:password \     https://www.example.com/mobile/resource


In addition to existing answers it is often desired to format the REST output (typically JSON and XML lacks indentation). Try this:

$ curl https://api.twitter.com/1/help/configuration.xml  | xmllint --format -$ curl https://api.twitter.com/1/help/configuration.json | python -mjson.tool

Tested on Ubuntu 11.0.4/11.10.

Another issue is the desired content type. Twitter uses .xml/.json extension, but more idiomatic REST would require Accept header:

$ curl -H "Accept: application/json"


From the documentation on http://curl.haxx.se/docs/httpscripting.html :

HTTP Authentication

curl --user name:password http://www.example.com 

Put a file to a HTTP server with curl:

curl --upload-file uploadfile http://www.example.com/receive.cgi

Send post data with curl:

curl --data "birthyear=1905&press=%20OK%20" http://www.example.com/when.cgi