{{theTime}}

Search This Blog

Total Pageviews

REST Tutorial



What is Rest?

A cacheable, stateless, client-server protocol, and stands for Representational State Transfer
-Rest is an architecture style for designing network applications
-Use HTTP connection instead of Complex CORBA, RPC
-Restful applications use HTTP requests to post, read and delete data (All CRUD Operations)

Programming Approach

Like Web Services, REST service is
-          Platform-independent (Linux/windows/mac)
-          Language independent(java, c#)
-          Http based standard

Security

-          No built in encryption, session management etc.,
-          For security username/password tokens can be implemented
-          For encryption REST can utilize HTTPS(secure sockets)

REST vs SOAP

SOAP Example to fetch user details.
<?xml version="1.0"?>
<soap:Envelope
  <od:GetOrderDetails>
   <od:OrderID>5000</od:OrderID>
  </od:GetOrderDetails>
 </soap:Body>
</soap:Envelope>

REST query looks

It's an URL using a simple GET.
Another query:
Again it is simple HTTP get used only for fetching the data, but not for CRUD.

Server Response Formats

REST Server response can be
               -XML
               - CSV
               -JSON (Java Script Object Notation)

Real time REST Services

-          Google Glass API
-          Amazon s3 Storage Solution
AJAX and REST
Ajax follows REST design principles and use XMLHttpRequest as a REST GET request.
Response is often a JSON.
REST Architecture:
-          Logical URLs (Resources)
-          No connection state
-          Can be cacheable
-          Proxy servers can be used
-          Do not use physical URL (Meaning refer the physical file, instead use logic names)
-          Any CRUD Operations should use POST request.

REST Java Example


Pseudo Code.  Implement the Exception handling correctly.
Get Request
public static String getRequest(String orderurl) throws IOException {
  URL url = new URL(orderurl);
  HttpURLConnection conn =
      (HttpURLConnection) url.openConnection();
 
  if (conn.getResponseCode() != 200) {
    throw new IOException(conn.getResponseMessage());
  }
 
  // Buffer the result into a string
  BufferedReader reader = new BufferedReader(
      new InputStreamReader(conn.getInputStream()));
  StringBuilder sbuilder = new StringBuilder();
  String line;
  while ((line = reader.readLine()) != null) {
    sbuilder.append(line);
  }
  reader.close();
 
  conn.disconnect();
  return sbuilder.toString();
}
 
Post Request
public static String postRequest(String orderURL, String[] paramName,
String[] paramVal) throws Exception {
  URL url = new URL(orderURL);
  HttpURLConnection httpConnection =
      (HttpURLConnection) url.openConnection();
  httpConnection.setRequestMethod("POST");
  httpConnection.setDoOutput(true);
  httpConnection.setDoInput(true);
  httpConnection.setUseCaches(false);
  httpConnection.setAllowUserInteraction(false);
  httpConnection.setRequestProperty("Content-Type",
      "application/x-www-form-urlencoded");
 
  OutputStream outputstream = httpConnection.getOutputStream();
  Writer writer = new OutputStreamWriter(outputstream, "UTF-8");
  for (int i = 0; i < paramName.length; i++) {
    writer.write(paramName[i]);
    writer.write("=");
    writer.write(URLEncoder.encode(paramVal[i], "UTF-8"));
    writer.write("&");
  }
  writer.close();
  outputstream.close();
 
  if (httpConnection.getResponseCode() != 200) {
    throw new IOException(httpConnection.getResponseMessage());
  }
 
  BufferedReader rd = new BufferedReader(
      new InputStreamReader(httpConnection.getInputStream()));
  StringBuilder sb = new StringBuilder();
  String line;
  while ((line = rd.readLine()) != null) {
    sb.append(line);
  }
  rd.close();
 
  httpConnection.disconnect();
  return sb.toString();
}

No comments:

Best Client Optimized Programming Language for Mobile Development?

Use Google's Dart language, which is Object-oriented, class-based and garbage-collected language with C-Style.  Dart can compile to eith...