What is Odata – Part 2 – REST Approach to HTTP

Introducing REST approach to Leverage HTTP

— This is the second blog in this ODdata series. You can read the first part of this blog here. – What is OData.

( Again Disclaimer : I am not a guru on protocol business. But following is my practical understanding and others may oppose this view but it has worked for me. If your views on this discussion is different or you have anything to add .. please feel free to comment on it– General feedback is also welcome. )


 

Ok so now we know that Data sent using the HTTP Protocol will be interpreted/accepted accordingly by the receiving device-smartphone, tablet etc. This data is usually sent out by the server in XML or JSON format. ( These are formats in which data is represented. )

For example :

JSON data

{
name: ‘James Bond’,
university: {
name: ‘Oxford’,
country: ‘UK’
},
gpa: 3.5,
score: ’10’
}

XML Notation

<student>
<name>James Bond</name>
<university>
<name>Oxford</name>
<country>UK</country>
</university>
<gpa>3.5</gpa>
<score>10</score>
</student>

To display this data the device uses applications which use HTML Language. Your internet browser for example, can therefore use HTML parsing to understand the data stream ( off course you will need to program the logic to decipher the XML or JSON first and then apply CSS rules to the data, before generating the proper HTML feed ) and beautifully display the webpage with colors, fonts and images.

Are you with me so far ?

OK!! Displaying a simple static page is fine, however what happens when you have to actually fetch real live data ? How will you manipulate the existing data on the webserver host.

For Example lets say there is a web server which is hosting the data for a student database.

In this database there is a table called STUDENTS and in the STUDENTS table there is a student record like this.

STUDENT_NUMBER = ‘007’

STUDENT_NAME = ‘James Bond’

Let’s say this web server is hosted in a far of country like Denmark ( Denmark is far of for me currently as right now I am in TX, USA – If you are reading this blog post from Denmark then think some other far off country  like USA ).

OK Now you want to fetch the details of the student James Bond and later on change this student’s Name from James Bond to Linkin Pereira. ( Yes, client requirements are weird like that — I know ). How will you do this? Here is where REST ( Representational State Transfer ) can help us to make a one to one mapping of CRUD operations to HTTP methods. CRUD stands for Create, Read, Update, Delete.

In other words REST approach is actually the implementation of HTTP or the way HTTP is meant to be used ( Protocol Gurus may not like my definition – but hey this is what I like — and this is my writing ).

In this case data is uniquely referenced by URL and can be acted upon using HTTP operations (GET, PUT, POST, DELETE, etc). 

According to this mapping

  • To create a resource on the server, use POST.
  • To retrieve a resource, use GET.
  • To change the state of a resource or to update it, use PUT.
  • To remove or delete a resource, use DELETE.

Though I would love to go on about REST protocol, a practical, simple example of this in action will be beneficial.

— So going back to our example of the Students Database. Lets say the university – OXFORD-UNIVERSITY has created a web service to allow third party softwares to access its student database.

  • In other words the University OXFORD-UNIVERSITY wants to make the names of its students public – so that everyone should have a way of accessing this resource. So you, me and anyone in the world should be able to read this data. And be able to build a webpage, mobile app or application to display this data.
  • You can read here to find out how various generic web-services can be developed – For SAP  specifically though, I will introduce you to SAP Netweaver Gateway Later.
  • So inorder to retrieve the students data the web service will make available a URL to a students list resource.
  • For example, a client(internet-browser- like Chrome, Firefox or IE) would use this web-address (URL) to get the students list: www.oxford-university.com/students
  • To retrieve James Bonds record you might then use a web url link, something like www.oxford-university.com/students/StudentDetails/007
  • Note that this isn’t the request body — it’s just a URL. This URL is sent to the web server using a simple GET request in your client application, and the HTTP reply is the raw result data — not embedded inside anything, just the data you need in a way you can directly use.
  • Also Note how the URL’s “method” part is not called “GetStudentDetails”, but simply “StudentDetails”. It is a common convention in REST design to use nouns rather than verbs to denote simple resources.

— Similarly to modify data on this server for this record you could use a PUT call.

SO without complicating this subject too much and without loosing focus, it should suffice to say that if you want to develop a web application to use and interact with this web service, then in your application code you need to use these kinds on calls like GET, POST, PUT and DELETE.

Using a provided web service to retrieve data and use it in your application is called Consuming the service.

So Now that you know what REST Approach on HTTP is what can it be used for.

Oh its limited to your imagine. Following are some web services I found interesting.

Here are examples of a few web services from well known service providers

Tesla Model S JSON API – 

If you are developing an Andriod APP to remotely start the Tesla Model S car for keyless driving.

Use the following web service request. Must start driving within 2 minutes of issuing this request.

https://owner-api.teslamotors.com/api/1/vehicles/vehicle_id/command/remote_start_drive?password=password

Parameters

  • vehicle_id = The id of the Vehicle. Example: 1. Number
  • password = The password to the authenticated my.teslamotors.com account
    Example: edisonsux.

Source : http://docs.timdorr.apiary.io/#reference/vehicle-commands/remote-start/remote-start

Flickr Service :

Flickr is a Photo Sharing Website. It provides various web services to enable developers to develop apps and websites to allow uploading and editing existing photos on their website.

Use the following web service request URL to POST an image to flickr programatically from your application. ( Remember as I said earlier – POST – data creation and edits – cannot be done directly from a URL – you need to write it in the web program using javascript )

https://up.flickr.com/services/upload/

https://www.flickr.com/services/api/response.rest.html

There are a huge set of services provided by Google – like Google Map to locate the Geographical coordinates of a point or locate a point on the map and then lots and lots from Twitter. etc.

So the idea is that the RESTful Api applications are limitless.

In the next part of this series – I will explain why we need OData.

7 Comments

  1. Nk

    Hi Linkin Pereira,

    The end to end ODATA service SAPUI5 document was excellent.

    Instead of using eclipse this should also be possible in WEB IDE?

    Thanks
    NK

  2. Deepak

    I just love this blog. Recently I got the requirement so I want to understand the OData. I guess now I can teach people 🙂

  3. Raj Kiran

    Nice blog. I wanted something like this to go through to start with OData.
    Small thing, REST stands for – Representational state transfer (Even I googled it 🙂 )

  4. Sonali

    Linkin… thank you & great job !!.. making it simpler to understand.. waiting for more post like this from you 🙂

Leave a Reply

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