Friday, December 14, 2018

What are resource in Web API

Ques :
Is the following URL a resource? If it is, what is the name of that resource and what is its representation?
The GET response of the URL should look something like:
[
   {
      id: 6,
      name: "John"
   },
   {
      id: 7,
      name: "Jane"
   }
]

Ans:

REST architecture treats every content as a resource. These resources can be Text Files, Html Pages, Images, Videos or Dynamic Business Data. REST Server simply provides access to resources and REST client accesses and modifies the resources. Here each resource is identified by URIs/ Global IDs. REST uses various representations to represent a resource where Text, JSON, XML. The most popular representations of resources are XML and JSON.

Representation of Resources

A resource in REST is a similar Object in Object Oriented Programming or is like an Entity in a Database. Once a resource is identified then its representation is to be decided using a standard format so that the server can send the resource in the above said format and client can understand the same format.
For example, in RESTful Web Services - First Application chapter, a user is a resource which is represented using the following XML format −
 
   1
Mahesh Teacher
The same resource can be represented in JSON format as follows −
{ 
   "id":1, 
   "name":"Mahesh", 
   "profession":"Teacher" 
}

Source : https://www.tutorialspoint.com/restful/restful_resources.htm

The URL is never a resource or its name or its representation.
URL just tells where the resource is located and You can invoke GET,POST,PUT,DELETE etc on this URL to invoke the resource.
Data responded back are the resources while the form of the data is its representation.
Let's say Your URL with given GET parameters can output a JSON resource - this is the JSON representation of this resource. While with other flag in the GET it could respond with the same data in XML - that will be another representation of the very same resource.
Another definition
Also the resource name is considered to be the 'script name', e.g. in this case it is users.jsonwhile this resource name is self describing the resource representation itself - when calling this resource we expect the resource is in JSON, while when calling e.g. users.xml we would expect the data in XML.
  1. When I change the offset parameter in GET the response contains different data set - is it a new resource or its representation?
  2. When I define which columns are returned in response in GET, is it a different resource or different representation, or?
  1. Well, here the problem and answer are clear - we still call the same URL, the server responses with the data in the same form (still it is JSON), data still contains information about users - just the information itself has changed due to the new offset parameter. So it is obvious that it is still the same resource with the same representation and the same resource name as before.
  2. Second problem could be a little confusing. Though we are calling the same resource, though the resource contains the same data (just with only predefined column set) and though the data is in the same representation it could seem to us as a different resource. But due to the points in the paragraph above it is nor the different resource or different representation. Though the data set contains less information the requesting side (filtering this data set) should be considering this and behave accordingly. So again: it is the same resource with the same resource name and the same resource representation.

No comments:

Followers

Link