WEB API

When I started my research “developing on mobile platforms using web technologies” I almost always found references to REST services. In this post I would like to gather the resources that I used to learn and to build one REST Service.

Why services for mobile platforms? Because in nowadays we can not live offline, we are always connected, reading news, emails, tweets, facebook stories and on a mobile device more then ever. We can see from here how the mobile browsing is growing, we have more the 5% growth in one year.

Why REST services? Because of what they are.(wikiREpresentational State Transfer (REST) is a style of software architecture for distributed systems such as the World Wide Web.

When it comes to services we can think about SOAP and REST. In the beginning there was SOAP. A Simple Object Access Protocol which has a lot of heavy date in its data request XML oriented.  Here is a very nice blog post about the differences between REST and SOAP. On google we can find hundreds of posts having this as a subject.

After reading a lot of them I realised that, if I need a light weight service API, scalable which will respect the HTTP protocol and will be language flexible I will choose REST. Founding this:(“ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.”) on WEB API page made me think about using the Microsoft Platform.

And it was very easy to use it. I installed all the needed tools from the previous website and opened up my Visual Studio. After that I clicked on Create New Project and chose  the ASP.NET MVC 4. In the next screen I had to choose WEB API and voila … I had my first REST service built with WEB API ready to run with GetAll, GetById, Post, Put and Delete implemented just for demo purposes. What is the first impression when we open that project is that we are in an MVC project with the models, the controllers and with App_Data and App_Start and also the ASP.NET MVC routing which are very helpful when we want to build an API. In the App_Start we will find the WebApiConfig.cs where we have the routes configured. And from here I understood how it works.

I started doing my first tutorial from the website. After creating my model and controller I started testing it calling the service endpoints from a webpage built with html and jQuery. And after that I opened it in Chrome and its developer tools for testing. An Ajax request looks like this:

// Send an AJAX request
$.getJSON("api/lessons/",
function (data) {
// On success, 'data' contains a list of lessons and we can display it.

for a service api endpont which is in the same website and which has a LessonsController with a method GetAllLessons without parameters.

// GET api/lessons
public IEnumerable GetAllLessons()
{
return Lessons;
}

And all of this because of the conventions and the WebApiConfig:

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

Another browser with very good developer tools is Firefox rest client. There you can see the requests, the body of it and the response.  Another desktop tool is Fiddler. The violinist, which has the Firefox rest client capabilities and much more. When it comes to tools I prefer Chrome for website testing and Firefox for quick rest testing. If I have a real problem and I am not able to figure it out with those two, then Fiddler will is my rescue.

In the next post we will play more with a REST API provided by Instagram.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>