Testing Rest API using CURL and Bash Scripts

What do we use for Testing Rest APIs, Advanced Rest Client, Jmeter? Sometimes it becomes so boring to do things in conventional way. Let’s try something different and walk away from the Mainstream Testers.

This post is all about breaking your limits and do some real coding. So let’s get started.

So what is CURL? It’s a command-line tool for transferring data using various protocols.Basically you can hit any URL and get it’s response.

Similarly we can execute an API and check for the HTTP response to find out whether the API is working as expected or not.

You need a Linux Machine to use CURL otherwise you can download in for your Windows and Mac OS from here: http://curl.haxx.se/download.html


curl –write-out “%{http_code}\n” –silent –output /dev/null “http://www.igyaan.in ”

The above code will print HTTP Status code in the terminal

Description of the option used (–write-out)

write-out – write-out is extremely useful. This option allows you to specify extra information that you want curl to print, using special variables. For example, to print the HTTP response code of your request, you use the “http_code” variable. You must enclose the variables in %{}.

silent – It allows one to suppress the progress output that curl prints when the command is run

output – It writes HTTP Status code in /dev/null file temporarily

What is Bash Script? Bash is a command processor, typically run in a text window, allowing the user to type commands which cause actions. Like all Unix shells, it supports filename wildcarding, piping, here documents, command substitution, variables and control structures for condition-testing and iteration.

And yes, you need a Linux Machine.

I will use the same CURL command from above and will create the bash script. Have a look at the simplest if else program to check the HTTP Status Code.



STATUS=$(curl –write-out “%{http_code}\n” –silent –output /dev/null “http://www.igyaan.in “)

if [[ STATUS -eq 200 ]]; then
echo “Success”
echo “Failed”

Save the file with a file name ending with .sh e.g. TestAPI.sh

Finally run this script with the following command:

bash TestAPI.sh

It’s a very simple program so I am not going to explain this. But please post comments for any queries

Hope you liked it and will eager to explore CURL and BASH scripts 🙂

4 thoughts on “Testing Rest API using CURL and Bash Scripts”

Comments are closed.