Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test web service using command line curl

I am building a web service for a web application, and I would like a simple tool to test this as I am developing. I have tried some firefox plug-ins (Poster, 'REST Client'), and even though these work fine I have been unable to upload files with them.

Also, I would rather have a command-line tool that I can use to easily write a set of integration tests for this web service and that I can send to consumers of this web service as an example.

I know that curl can work for this but would like a few examples, especially around authentication (using HTTP Basic) and file uploads.

like image 703
Mauritz Hansen Avatar asked Nov 17 '11 12:11

Mauritz Hansen


People also ask

How do I test my website using curl command?

The syntax for the curl command is as follows: curl [options] [URL...] In its simplest form, when invoked without any option, curl displays the specified resource to the standard output. The command will print the source code of the example.com homepage in your terminal window.

What is curl in web services?

cURL, which stands for client URL, is a command line tool that developers use to transfer data to and from a server. At the most fundamental, cURL lets you talk to a server by specifying the location (in the form of a URL) and the data you want to send.

How do you test a SOAP Web service with curl?

To test a SOAP web service, we just need to make HTTP requests with a SOAP envelope in the request body. We don't need to specify that we're using HTTP because it's the default protocol in cURL. Since we're testing the request, we use the verbose mode via the -v option.


2 Answers

Answering my own question.

curl -X GET --basic --user username:password \      https://www.example.com/mobile/resource  curl -X DELETE --basic --user username:password \      https://www.example.com/mobile/resource  curl -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 
like image 160
Mauritz Hansen Avatar answered Sep 21 '22 11:09

Mauritz Hansen


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" 
like image 20
Tomasz Nurkiewicz Avatar answered Sep 21 '22 11:09

Tomasz Nurkiewicz