Restful Webservices

Restful Webservices

Visibility

For RESTful web services, your key goal must be to maintain visibility to the extent possible. Keeping visibility is simple. Use each HTTP method such that it has the same semantics as specified by HTTP, and add appropriate headers to describe requests and responses.

Another part of maintaining visibility is using appropriate status codes and messages so that proxies, caches, and clients can determine the outcome of a request. A status code is an integer, and the status message is text.


When to trade Visibility

Whenever you have multiple resources that share data or whenever an operation modifies more than one resource, be prepared to trade visibility for better abstraction of information, loose coupling, network efficiency, resource granularity, or pure client convenience.

situations where you may need to give up visibility for other benefits:
Client convenience
Servers may need to design special-purpose coarse-grained composite resources for the sake of client convenience.
Abstraction
In order to abstract complex business operations (including transactions), servers may need to employ controller resources to make changes to other resources . Such resources can hide the details used to implement business operations.
Network efficiency
In cases where a client is performing several operations in quick succession, you may need to combine such operations into batches to reduce network latency.



Maintain Application State

Encode application state into URIs, and include those URIs into representations via links . Let clients use these URIs to interact with resources. If the state is large or cannot be transported to clients for security or privacy reasons, store the application state in a durable storage (such as a database or a filesystem), and encode a reference to that state in URIs.

Type 1:



    ...
  
  
    ...
  
  
    ...

id-until>2009-10-02
1
1
A link containing application state
In this example, the server stores the quote data in a data store and encodes its primary key in the URI. When the client makes a request to purchase insurance using this URI, the server can reinstate the application state using this key.


Alternatively, if the amount of data for the quote is small, the server can encode the state within the URI itself, as shown in the code below.


# Request
GET /quotegen?fname=...&lname=...&... HTTP/1.1
Host: www.example.org

# Response
HTTP/1.1 200 OK
Content-Type: application/xml;charset=UTF-8


  
    ...
  
  
    ...
  
  
    ...
    2009-08-02
    
  


 Since the client will need to send all that data back in every request, encoding the application state in links may reduce network performance. Yet it can improve scalability since the server does not need to store any data, and it may improve reliability since the server does not need to use replication. 


 

 




Ref :Restful Webservice CookBook