Thursday, 22 March 2018

RESTful Web Services Tutorial 2 - Setting up a REST Client

In this lesson, we would cover the following
  1. Review of Tutorial 1
  2. Overview of REST Client
  3. Setup of Postman REST Client
  4. Getting used to Advanced REST Interface
  5. Performing GET Operations
  6. Next Steps 


1. Review of Tutorial 1


 In Tutorial 1, we build a REST Web service from the scratch using Visual Studio and C#. It was quite a straight forward procedure. In case, you need to review it, that would be fine so you can get used to it.
Also, if you have not taken Tutorial 1, you need to go back and take it because you need it to get a clearer understanding of this Tutorial 2.


2. Overview of REST Client


I am sure that by now the concept of REST web service is very clear, a service that is accessible over HTTP and conforms to the REST architechtural style.

So the question is,
"what is the difference between a REST web service and other services?"
the differnce is that for a service to be considered a REST web service, it should conform to the following 4 criteria
  1. it must have resource-based URLs - Level 1
  2. it must use the correct HTTP verbs (GET, PUT, POST, DELETE) - Level 2
  3. it must return the right status-codes - Level 2
  4. it must be HATEOAS-compliant - Level 3
This four criteria make up the Richardson's Maturity Model. You can read it up here.

For us to test these wether our API meets up these four criteria, just thebrowser would not do. We need a REST Client application

3. Setup of Advanced REST Client


In this demo, we are going to use Advanced Rest Client from now till the rest of this tutorial. Advanced Rest Client is a free application provided as a plugin to Chrome browser.

Visit the Chrome Store on this link https://chrome.google.com/webstore/category/extensions
Search for Advanced REST Client. Then select it.
The window is shown as in Figure 1

Figure 1: Advanced REST Client

Click on Add To Chrome on the upper left (Blue Button). It downloads and adds as an extention to chrome.
You will see another tab opens and you can see Advanced Rest Client (a blue Icon) in the list of apps as shown in Figure 2

Figure 2 - Advanced REST Client Added to Chrome

Click on it and then Click on Launch App in the next window that opens
The Advanced REST Client window opens as shown in Figure 3

Figure 3: Advanced REST Client Window

Let's now take some time to get used to the Advanced REST Client interface


4. Getting used to Advanced REST Interface


Take not of the following parts of the Window
Method List: This is the list of HTTP methods. The first item here is GET but you could drop down to see a list of other methods. You need to select this depending on the type of request you want to make

Request URL: This is where you enter the URL containing the API you want to access. The Request URL ends with the port number if ther is a port number

Path: This is path that to a resource which may come after the Request URL. This path is everything following the port number.

Query Parameters: This is URL query parameters. It is used if the request contains query paramters.

For example
http://kindsonthegenius.blogspot.hu/getpost?id=50

In the above example the URL parameter is id and the value is 50.

Send Button: Click to sent the request

Headers: HTTP headers define the operating parameters or a HTTP transaction.For example, encoding and character set.

Body: Request body. This tab shows for POST, PUT and DELETE request types. It appears between the Headers tabe and the variables tab.

Varibles: This is needed if there are additional variables needed to be sent along with the request


5. Performing GET Operations


You could see how it works by entering www.google.com in the request URL section and pressing Send. You will observe that you have a response of 200 OK.

We are now going to use Advanced REST Client to test the API we built in Tutorial 1.
Open the Solution we created in Tutorial 1

(Download the Solution from here)

Run it and make sure it it running

We would carry out to REST API requests:
  • GET request to return all employees
  • GET request to return employees by a given ID

For the first task: In the Request URL section, enter the URL (make sure to change the port number, 24675 to the one you have in your browser)

http://localhost:24675/api/Employee/getEmployees

Your output would be as shown in Figure 4

Figure 4: GET Request for all Employees

For the second task: In the Request URL section, paste the URL below (make sure to change the port number, 24675 to the one you have in your browser)

http://localhost:24675/api/Employee/getEmployeeById/2

Observe that the the URL is automatically divided into two paths
  • Request URL: http://localhost:24675
  • Path: /api/Employee/getEmployeeById/2

You will get similar output, but with just one record as shown in Figure 5

Figure 5: GET Request with for particular ID

If you got this correctly, then be sure you will reach great heights! You have successufully completed Tutorial 2. Let's keep moving gradually untill you become an expert in REST APIs!


6. Next Steps 


Now that you have completed Tutorial 2 of RESTful Web Service, congratulations! Note that in we have written a REST API that accepts only a GET request.
In the next tutorial, we would write more actions (remember actions are simply methods or functions) that would accept a POST request and create a new Employee object.
Then we would also write another method to perform a PUT operation and so on

For now, thanks for reading and do leave a comment to let me know your observations 😅😃