An Introduction to RESTful Web Services

REST stands for Representational State Transfer

Like in any other product's successful development, an architectural design plays an important role and it is mostly for describing the features of how that product will be built. Software also demands such kinds of architecture or we can architectural design that simply by building an environment of software by describing the features that are the guidelines to implement the software.

REST Background

The concept of REST was first coined by Roy Thomas Fielding in 2000, he was one of the people working on HTTP. The reason why I am providing a little information about Fielding is only because our REST-based services are also implemented on a specification on which the internet depends, HTTP. REST is built on a client-server architectural style.

To learn how REST works we should understand how the web works, since REST uses the web, specifically HTTP.

HTTP Verbs

To interact with resources and their representations REST uses the HTTP communication protocol that defines a set of standard method (verbs), headers and status codes like 404, 402 and so on.

  • HTTP Verbs
    • GET
      GET
      basically requests a resource representation.
    • POST
      POST
      actually works for submitting the data that must be processed at an endpoint (resource).
    • DELETE
      DELETE
      allows to delete a resource.
    • PUT
      Create or update a resource.
    • HEAD
      Use to retrieve the header, its works similar to GET, in other words to retrieve something. The only difference is HEAD retrieves only headers but GET retrieves the body.
    • OPTIONS
      Returns supported methods by resource.
    More precisely, GET, POST, DELETE and PUT provide the basic CRUD operations for the web whereas HEAD and OPTIONS provide the ability to retrieve metadata (MSDN).
  • HTTP Status Codes


Constraints of REST
  • resources
  • URI

In REST what actually happens is that the user interacts with resources via a unique Uniform Resorce Identifier (URI). A URI is how named or represented resources are addressed. Resource interaction ids are done using HTTP verbs (GET, POST, PUT and DELETE), we can also include resource types (XML, JSON, JPG and PNG) as HTTP content.

Let's create a RESTFul service.

Create > New Project > WCF > WCF Service Application



Delete the services created by default.

Add your own service. Go to Project then select Add > Add New Item.

Add WCF Service



WebGet and WebInvoke

To make the service contract called using the HTTP Protocol, in other words Rest Protocol, we need to add WebGet and WebInvoke attributes that may include properties like RequestFormat, ResponseFormat, Method and UriTemplate. If you can't find the WebGet and WebInvoke attributes then you need to add a System.ServiveModel.Web DLL reference.

The WebGet attribute maps to HTTP GET requests whereas the WebInvoke attribute is mapped to HTTP POST by default, it can also be set for other HTTP methods like DELETE and PUT.

  • UriTemplate basically maps HTTP parameters with a parameter of the service contract.
  • RequestFormat
    WebMessageFormat.Xml (by default)
    WebMessageFormat.Json
  • ResponseFormat
    WebMessageFormat.Xml (by default)
    WebMessageFormat.Json
We can have either of the following two formats:



Add the following contract in IRESTFulDemo to get the DefaultData format, XML



And in RESTFulDemo.svc.cs:



For JSON format:

Implementation



Binding



Closure

Now our REST service is ready.I hope you like it. Coments are welcome.

Happy Learning ! 

Up Next
    Ebook Download
    View all
    Learn
    View all