Testing REST with cURL

Nowadays is very common for developers, especially for back-end developers, develop some REST services to be consumed by the front-end developers. At the end of the development cycle usually you have (or you should have) some beautiful test suits that allow you to test your services and check if everything is working well. But, what happen during the development? The developer usually needs to test the services punctually, needs to test the connectivity or needs to perform a few test to check the REST API that he is investigating or trying to consume. There are some different solution to do this, create a little application to do it, use one of the plugins for the different browsers to use REST services, or use cURL. In this post, I am going to explain how to use cURL to perform these little checks in a quick way.

If you do not know cURL, cURL is a command line tool and library for transferring data with URL syntax, supporting a lot of different formats and options. Usually, it is installed by default in Unix and Unix based systems. In windows systems you need to install it or if you are using Cygwin you probably have it installed.

The first step before to start to use cURL should be check if it is installed in our system and take a look to the manual or help information that the tool provide us.

Checking if cURL is install in our system:

curl

This command should returns us something like:

curl: try 'curl --help' or 'curl --manual' for more information

Now, we can read the information about the tool and how to use it, but this information is huge and for this reason I am going to explain below the options that we are going to need. To read the information about the tool we have some different options:

man curl
curl --help
curl --manual

After read all this documentation the main options that we are going to need to test a basic REST services implementing CRUD operations are:

  • i – shows response headers.
  • H – passes request headers to the resource.
  • X – passes the HTTP method name. Default option is GET.
  • d – passes in parameters enclosed in quotes; multiple parameters are separated by ‘&’.

Then, it is time to start with the example.

In this case, we have available some REST services implementing the CRUD operations for Books. The available verbs are:

  • GET: Recover all the items or a particular item.
  • POST: To create a new book.
  • PUT: To update the book information.
  • DELETE: To delete a concrete book.

The available URLs are:

  • /books (GET, POST)
  • /books/{id} (GET, PUT, DELETE)

The object book will be a very simple object with three properties:

  • id: It will be auto generated by the back-end.
  • author: It should be provided in creation time.
  • title: It should be provided in creation time.

Having this clear in our minds we can start.

Recover all the books (/books, GET) It is empty right now

curl -i -H "Accept: application/json" http://localhost:4567/books
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Content-Length: 0
Server: Jetty(9.0.2.v20130417)

Create a new book (/books, POST)

curl -i -H "Accept: application/json" -X POST -d "author=john&title=unknown" http://localhost:4567/books
HTTP/1.1 201 Created
Content-Type: text/html; charset=UTF-8
Content-Length: 10
Server: Jetty(9.0.2.v20130417)

1354591741

Recover a concrete book (/books/{id}, GET)

curl -i -H "Accept: application/json" http://localhost:4567/books/1354591741
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Content-Length: 28
Server: Jetty(9.0.2.v20130417)

Title: unknown, Author: john

Update a book (/books, PUT)

curl -i -H "Accept: application/json " -X PUT -d "title=Sunshine" http://localhost:4567/books/1354591741
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Content-Length: 33
Server: Jetty(9.0.2.v20130417)

Book with id '1354591741' updated

Recover updated book (/books/{id}, GET)

curl -i -H "Accept: application/json" http://localhost:4567/books/1354591741
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Content-Length: 29
Server: Jetty(9.0.2.v20130417)

Title: Sunshine, Author: john

Delete a book (/books/{id}, DELETE)

curl -i -H "Accept: application/json" -X DELETE http://localhost:4567/books/1354591741
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Content-Length: 33
Server: Jetty(9.0.2.v20130417)

Book with id '1354591741' deleted

Recover deleted book (/books/{id}, GET)

curl -i -H "Accept: application/json " http://localhost:4567/books/1354591741
HTTP/1.1 404 Not Found
Content-Type: text/html; charset=UTF-8
Content-Length: 14
Server: Jetty(9.0.2.v20130417)

Book not found

With all the previous instructions we can test and check REST services implementing CRUD operations. It is a very easy and quick way, and if you use to use the console or terminal you will find it easier than use other tools.

Obviously, there are more ways to use it, for example, if your REST services expect a file, but the purpose of this post was only do a little introduction to the tool and to have a place to check the syntax quickly.

See you.

Advertisements
Testing REST with cURL

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s