How to use curl scripts to test RESTful web services (GET, POST, etc.)

Summary: This tutorial shows how to use Unix/Linux curl command to create shell scripts to test REST/RESTful web services.

Background: How to test REST services and microservices

There may be better ways to do this, but when I was writing a mobile app — with a JavaScript client written in Sencha Touch, and the server written with the Play Framework — I wrote some Unix curl scripts to simulate GET, POST, DELETE, and PUT request (method) calls to my server-side Play Framework REST/RESTful web services.

You can also write REST clients with Scala, Java, and other languages, but for various reasons I wanted to test these web services with curl. As an added bonus, you can include scripts like this into your testing and integration process, if you like. You can also use the same approach with Nagios to make sure your service is still running.

curl command GET, POST, DELETE, and PUT examples

The following examples show the code for each of these curl scripts. First, here’s get.sh:

curl --request GET http://myhost/notes/rest

Next, here’s post.sh:

curl \
  --header "Content-type: application/json" \
  --request POST \
  --data '{"title": "Test Title", "note": "Test note"}' \
  http://myhost/notes/rest?_dc=1379027286060

Here’s delete.sh:

curl \
  --header "Content-type: application/json" \
  --request DELETE \
  --data '{"id": 1, "title": "Test Title", "note": "Test note"}' \
  http://myhost/notes/rest

Finally, here’s put.sh:

curl \
  --header "Content-type: application/json" \
  --request PUT \
  --data '{"id": 100, "title": "Test Title", "note": "Test note"}' \
  http://myhost/notes/rest?_dc=1379027286060

A note about Sencha

One note regarding the _dc part of those URLs. That’s a Sencha convention, where dc stands for “disable cache.” I have since learned that you can get rid of that part of the URL by adding code like this to your Sencha store:

proxy: {
    type: 'rest',
    url: '/projects',
    enablePagingParams: false,
    limitParam: false,
    noCache: false,    // this setting gets rid of the _dc url parameter
    startParam: false
}

I kept the other parameters inside the proxy block, but the one that gets rid of the _dc parameter is the noCache setting.

More curl POST examples

For a slightly different POST example that uses JSON, I used this curl script to test a PHP script named login.php:

curl \
  --header "Content-type: application/json" \
  --request POST \
  --data '{"user": "alvin", "password": "foobar"}' \
  http://localhost:8888/finance/php/login.php

I ended up changing the login.php script so it wouldn’t use JSON, and my resulting curl script looked like this:

curl \
  --request POST \
  --data "user=alvin&password=foobar" \
  http://localhost:8888/finance/php/login.php

All I had to do there was remove the --header option and change the data format.

curl command: Headers returned by the server

To see the headers sent back by the server, add the -D- option to your curl command:

curl \
  --request POST \
  --data "user=alvin&password=foobar" \
  -D- \
  http://localhost:8888/finance/php/login.php

That option tells curl to dump the headers to STDOUT, so when I run this script against login.php, which creates a session on the server side, I see this output:

$ ./post.sh

HTTP/1.1 200 OK
Date: Tue, 18 Mar 2014 02:17:12 GMT
Server: Apache/2.0.63 (Unix) PHP/5.3.2 DAV/2
X-Powered-By: PHP/5.3.2
Set-Cookie: PHPSESSID=cf8159e6aa1294b70505870d4a2261c5; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 44
Content-Type: text/html

{"success":true,"msg":"User is authenticated."}

The last line shows the JSON returned by the server, and everything before that is header information.

Discussion

I used the GET, POST, DELETE, and PUT methods because when I first started with Sencha Touch, it seemed like the best way to go. Since then I’ve learned that there are other ways to do the same thing with Sencha, but this works okay, and for the purposes of this blog post, it shows how to use each method with curl.

After making them executable, I run these little scripts from my operating system command line, like this:

$ post.sh

When I run the script, I may see output in this terminal window, and I may also see output in the Play Framework window, depending on what I’m logging.

Summary

I’m sure there are many other ways to do this, but I’m a command-line guy, and it works well for me. It’s fast, it can be scripted, it lets me send JSON to my REST services, and it lets me test my services before accessing them from the Sencha Touch client.

If you wanted to see how to use the Unix/Linux curl command for web service testing, I hope this is helpful.