Using curl scripts to test RESTful web services

There may be better ways to do this, but as I’m writing a mobile app with the client written in Sencha Touch, and the server written with the Play Framework, I’ve written some curl scripts to simulate GET, POST, DELETE, and PUT request (method) calls to my RESTful Play services.

GET, POST, DELETE, and PUT examples

The following examples show the code for each of these 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

More POST examples

For a slightly different POST example that uses JSON, I was using 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

We 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.

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, but this works okay, and for the purposes of this blog post, it shows how to use each method with curl.

I just run these scripts from the 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, etc.

Summary

I don’t know if this is a great idea, but I’m a command-line guy, and it works well for me. It’s fast, 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 curl command for web service testing, I hope this has been helpful.

Post new comment

The content of this field is kept private and will not be shown publicly.