Sharing the things I learn day by day

Wednesday, December 25, 2013

INTRODUCTION TO WEB API (PART 1)

Hi All,

This blog post will demonstrate on how to create a RESTful web service using Web API, which is introduced by Microsoft with the MVC 4 release. 


I’m using Visual Studio 2012 for this demonstration.


First I will create a New Project and I will select a MVC 4 Web Application. I name it as WebAPIDemo.



Next you will be prompted to select the template, and you can see that there is a template for Web API as well. This template will give you a class in which the HTTP methods are implemented which can run and test immediately. It will also provide a MVC web application which can be used to test your Web API.

But for this demonstration I will not be using the Web API template. I will use the Basic template. I will not be testing the Web API using a MVC application. I will show you some tools available in which RESTful services can be tested.




Web API works on the MVC principle. The way how to invoke a method in the Web API is the same as in MVC.

So next I will create a controller and will name at as StudentController. From the template I will select “Empty API Controller”.




All Web API controller classes extend from the ApiController class. In StudentController, I will write four methods to Insert, Update, Search and Delete to represent the CRUD oprations.


CRUD Operation
Method Name
Insert
InsertStudentDetails
Delete
DeleteStudentDetails
Update
UpdateStudentDetails
Select
SelectStudentDetails
GetAllStudents

I will create a class called Student which has four attributes. Place the Student class inside the Models folder.


The content of the class is shown below.


Now let’s implement the four methods. Since I’m not going to use a database to store and retrieve values, I will hardcode student details in a list and use it. The list is as below.


The tool I’m going to use to test the API is “RESTClient” Download. This is a tool for Firefox. If you are using chrome then you can use “Postman” Download. You can just run the application and test it in these tools.



First I will implement the GetAllStudents method which returns all the students in the list.




[HttpGet] attribute indicates to the Web API that this is a HTTP GET method. So below is the result after invoking the method.

Request URL: http://localhost:3625/api/Student/GetAllStudents

Note: You need to change the port number accordingly when you try this out in your machine.


Request:






Response:

The response contains the three student details I hardcoded in the list.

Next I will implement the SelectStudentDetails method which accepts the studentID as the parameter.




Request URL: http://localhost:3625/api/Student/SelectStudentDetails?studentID=1

Here when invoking the method the studentID is passed.

Response:














For rest of the demo please read INTRODUCTION TO WEB API (PART 2)

INTRODUCTION TO WEB API (PART 2)


<<INTRODUCTION TO WEB API (PART 1)

Hi,

In this post I will implement rest of the methods.

Now I have the select methods implemented. Now I will implement the InsertStudentDetails method. 


















As you can see I have invoked another method to get the student data that is been passed through the request. The method implementation of GetRequestData is shown below.









In this method I’m getting the data in the request as a string. Since I’m passing the data in Json format I need a Json deserializer. For that I’m using the Json framework for .NET which can be downloaded from here: http://james.newtonking.com/json . Then I can convert the data into a Student object. The Json which is passed in the request is shown below.

{
        "StudentID" : 4,
        "StudentName" : "Nipuni",
        "Age" : 35,
        "Address" : "Colombo 5"

}

Request URL: http://localhost:3625/api/Student/InsertStudentDetails

Set the content type of the header as application/json.
























Add the Json to the body section.

Request:














Response:













Now if the GetAllStudents methods is called you can see the newly inserted record to the List.

Next I will implement the UpdateStudentDetails Method.

























This method also uses the GetRequestData Method to get the details of the student that needs to be updated. Then I search for a matching record with the student ID. If a record is found then I update it or else I sent back a response message Not Found.

The why how the message is invoke is the same as the insertStudentDetails Method.

Request URL: http://localhost:3625/api/Student/UpdateStudentDetails

Next I will implement the DeleteStudentDetails method.











I have searched for the student using the student ID, and if it’s found I delete that student and return true or else return false.

Request URL: http://localhost:3625/api/Student/DeleteStudentDetails?studentID=1

Once a delete operation is done if you run the GetAllStudents method you can see whether the student is deleted.

That's all for this blog,
Happy Coding.