Search This Blog

Thursday, October 8, 2015

REST API - Fundamentals

REST is known as Representational State Transfer meaning a Representational State of the resource(Image, html document, etc) is transferred from the Server(Web Server) to Client (Web Browser). REST is an Architectural Style to design network applications. REST also has Architectural constraints, but they are not enforced. For example, in REST, POST is supposed to create a new record in the server. But we can use POST to query records from the server.

SOAP is a XML based RPC (Remote Procedure Call) over HTTP protocol using HTTP Post method. Also SOAP can be used with FTP, JMS and SMTP Transport protocols.

REST-based architectures communicate primarily through the transfer of representations of resources". This is fundamentally different from the Remote Procedure Call (RPC) approach that encapsulates the notion of invoking a procedure on the remote server. Hence, RPC messages typically contain information about the procedure to be invoked or action to be taken. This information is referred to as a verb in a Web service request. In the REST model, the only verbs allowed are GET, POST, PUT, and DELETE. In the RPC approach, typically many operations are invoked at the same URI. This is to be contrasted with the REST approach of having a unique URI for each resource.

REST has the following architectural constraints:
stateless
cachable
uniform interface (only through GET/POST/PUT/DELETE)
client-server
layered system


REST vs SOAP:
SOAP uses only POST
REST uses GET, POST, PUT, DELETE
SOAP uses only XML Message
REST uses XML, JSON, Text, csv etc.
In SOAP, Interface definition is defined in WSDL file.
In REST, there is no interface definition file. Data can be set in HTTP parameters (URL Query parameters) of GET or in HTTP Body(payload) of POST.

REST Service format:
REST service has three components:
1. Service name (or) Service Endpoint (maps.googleapis.com)
2.Unique URI(path) to identify the Resource (/maps/api/geocode/xml)
3. Query parameters if it's GET method (?param1=sx, param2=123)

Example:
Resource 1: http://maps.googleapis.com/maps/api/geocode/xml ? param1=sx, param2=123
Resource 2: http://maps.googleapis.com/maps/api/geocode/json

Sample REST raw GET request:

GET http://maps.googleapis.com/maps/api/geocode/xml?address=Parkway%2C%20Mountain%20View%2C%20CA&sensor=false HTTP/1.1
Accept-Encoding: gzip,deflate
Host: maps.googleapis.com
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

Sample REST Raw GET Response:

HTTP/1.1 200 OK
Date: Thu, 15 Oct 2015 09:08:16 GMT
Expires: Fri, 16 Oct 2015 09:08:16 GMT
Cache-Control: public, max-age=86400
Vary: Accept-Language
Access-Control-Allow-Origin: *
Content-Encoding: gzip
Server: mafe
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Content-Length: 555
Content-Type: application/xml; charset=UTF-8
Connection: keep-alive

XML Document>
 .......
...........

Sample SOAP Web Service Request (always use POST method):



No comments: