Monday, 26 March 2018

RESTful Web Services Tutorial 4 - Testing the REST APIs using Advanced REST Client

Good to see you again! It is good to know that you have come this far. Thumbs up to you! 👍👍👍👍


In this tutorial, we would cover the following topics
  1. Review of Tutorials 2 and 3
  2. About REST Clients
  3. Important Concepts of REST Client
  4. Test the POST Request
  5. Test the PUT Request
  6. Test the DELETE Request
  7. Next Steps - Using Data
If you have any difficulty following this tutorial, explain in the comment box below and I would give you the needed support👇👇👇👇


1. Review of Tutorial 3


In Tutorial 3, we successfully creates all the methods for the CRUD operations (GET, POST, PUT and DELETE). Meanwhile CRUD means Create, Read, Update and Delete.
You could go to Tutorial 3 if you have not done so because this lesson follows from Tutorial 3. 


2. About REST Clients


A REST Client allows you to test RESTful APIs by providing a way of makings a request to the API and specifying neccessary criteria needed to conform to the RESTful architecture. These features provided by a REST client is not readily available in a normal browser application. Two important

REST Clients, which I use and are free are:
  • Postman REST Client
  • Advanced REST Client
In this tutorial, we would use Advanced REST Client(ARC)

3. Important Features of REST Clients


Remember that to do a GET request, we simply specify the Request URL and select the GET Method. But in this case, we need to do a POST request which requires specifying additional attributes.
These are:

1. The Request Body
This is the body of the request which contains the data to be used to create a new resource(in our case, a new Employee).
The Request Body shows up when you select POST as the method in the methods list. It apears as a tab between Headers and Variables.

2. Body Content Type
This specifies the type of format of the data you want to pass accross in the body of the request

3.Request-Header
The Header (or HTTP Request Header) allows the client to pass additional information along with the request to the API.


Figure 1: POST Request Body


4. Test the POST Request


To carry out this task, be sure that you have completed Tutorial 3. Follow the  steps below.
Step 1: Open the Employee application we created in Tutorial 3
Step 2: Run it by pressing F5
Step 3: Make sure  that the browser window opens up

Step 4: Lunch Advanced Rest Client (you can launch by entering this link in Chrome https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo because the Advanced REST Client is a Chrome extension)
Step 5: Select POST from the list of methods
Step 6: Enter this POST Request URL, which is :

http://localhost:24675/api/Employee/AddEmployee

Also make sure you use the correct port number which you can get from your browser

Step 7: Now enter the body of the request to create a new Employee. I have created it below, but you can change to any values you wish


 {
"EmployeeId": 5,
"Name": "Kindson",
"City": "Akokwa"
}
Listing 1: Request Body of POST Request

Step 8: Change the Body content type to application/json
Step 9: Click on the Header tab and Click on Insert headers Set. A default headers set is displayed
Step 10: Click on USE THIS SET under it. The screen is shown in Figure 2

Figure 2: Inserting Headers Set


At this point, you are now ready to send the POST request

Step 11: Click on the SEND button

If you have followed the steps correctly, you should have a response code:

201 Created

If you don't have this result, let me know by leaving a comment in the comment box below.


5. Test the PUT Request


The PUT request is used to edit an existing record. What normally do is to display the record I want to edit, then copy it and use it as the body of the PUT request.

The steps you will follow is similar to the steps for the POST request. I would only outline the few differences:

Step 1: Make a GET Request to the employee of Id = 1

http://localhost:24675/api/Employee/Employees/1

Step 2: Highlight an copy the record returned by the request
Step 3: Select PUT from the methods list instead of POST

For the PUT request, use the URL

http://localhost:24675/api/Employee/AddEmployee/1

assuming we want to edit the employee record with Id of 1

Step 4: Paste the request body without the Id
Step 5: Make the needed changes in the record in the request body.
Step 6: Click on SEND to complete the request

6. Test the DELETE Request


For the DELETE request, take note of the following points as it is very similar to the PUT request but without a request body.

Step 1: Select DELETE in the methods list

Don't specity anything in the Request Body (to delete the entire record)
Step 2: Enter the DELETE request URL is given by

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

in case you want to delete the employee record with Id of 2

Step 3: Click on SEND

Quiz: What response did you get for the DELETE request?
Leave your answer in the comment box below

Any problem: If you have any difficulty following this tutorial, explain in the comment box below and I would give you the needed support.

7. Next Steps - Using Data


You can observe that the changes we make throught the REST requests are not saved back to anywhere. This is because we have not implemented any functionality to store our data.
The current data we use are hard-coded into the the controller file. In the real production application, you would never have to do anything like this.
Therefore in the next tutorial, Tutorial 5, we would set up out application to work with MSSQL database.
Then in Tutorial 6, we would generate the equivalent class diagram in Visual Studio

For now I would like to thank you for the time you spend learning!