Professional Documents
Culture Documents
<Emp>
http://service.com/emp/123 <Name>ABC</Name>
<Id>321</Id>
http://service.com/emp/{id} <Email> abc@domain.com</Email>
http://service.com/emp? Id=1 <Org>Infosys</Org>
</Emp>
Resource
(Nouns)
Representation
(XML, JSON)
REST API
{
“Name": “ABC",
“Id": “321",
Verbs GET http://service.com/emp/123 HTTP/1.1 "Email": ”abc@domain.com",
(GET, PUT, “Org": “Infosys"
POST http://service.com/emp/123 HTTP/1.1
}
POST)
DELETE http://service.com/emp/123 HTTP/1.1
Resources but HTTP verbs only tell you which action understanding, use nouns for every
needs to performed on the host. There are resource. Additionally, do not mix up
Resources are the fundamental elements
many actions that a client can trigger on singular and plural nouns, and always use
of the web platform. While working
the host. plural nouns for consistency:
on REST, the first task is to identify the
These verbs are –
resources and find out how they are linked GET parts/1
with each other. Every resource has a • GET: retrieve an existing resource GET orders/123
unique identifier on the web platform, • POST: create a new entry of resource GET seats?id=3
which is known as the universal resource • PUT: modify an existing resource
identifier (URI) and the best example on • DELETE: remove an existing resource How to handle asynchronous tasks
the Web is a uniform resource locator (URL). The Hypertext Transfer Protocol (HTTP) is a
There is no limit on the number of URIs that Representation synchronous and stateless protocol.
can refer to a resource. For example we can The server and client get to know each
The third and final wheel is about
access a particular domain page (of course, other during the current request. After
determining a way to showcase these
a resource) using http://yahoo.com and this, both of them forget about the
resources to clients. REST supports all
http://www.yahoo.com. request. Because of this behavior, retaining
formats without any restrictions; so you
information between requests is not
In REST web services, we use nouns to can use any format for representing the
possible at the client and server-side.
identify a type of resource. Employee resources.
information from EmpDB can be accessed For asynchronous requests (that take too
Based on the client’s and server’s ability
using the below long to complete) follow the steps detailed
to work with the formats, you can go with
URL:http://EmployeeService/Employee/1 below –
JSON, XML, or any other format.
• Place a GET / Post request which takes
Verb
too long to complete
Verb is an HTTP action like POST, GET PUT, • Create a new task and return status
DELETE, OPTIONS, etc. Best Practices code 202 with a representation of the
Let’s first revisit the HTTP Request. Here we come up with a few new resource so the client can track the
recommendations / best practices that can status of the asynchronous task
Example of a GET Request:
be used to develop flexible, easy-to-use, • On completion of the request, return
and loosely coupled REST APIs. response code 303 and a location
GET http://www.w3schools.com/ : HTTP/1.1
header containing a URI of resource that
Status: HTTP/1.1 200 OK
Accept text/xml,text/html; Use nouns for Resources displayed the result set
Accept-Encoding gzip, deflate, sdch and not verbs • On request failure, return response code
Accept-Language en-US,en; 200 (OK) with a representation of the
Verbs should not be used for resources
task resource informing that the process
because doing this will give a huge list
Using URLs, the identity of the target server has failed. Clients will look at the body
of URLs with no pattern – which makes
can be determined for communication, to find the reason for the failure.
maintenance very difficult. For easy
Here, an example is provided for a file-
upload web service, which supports
asynchronous model.
# Request
POST /files/ HTTP/1.1
Host: www.service.com
Server
Select cameras which were released in the year 2015, in ascending order http://www.service.com/Cameras?year=2015& sortbyASC=release date
# Request other location on the same or a different In this example, a request is placed to
POST /parts/engine/copy;t=<token> server. The original resource should also be merge one part with another part:
HTTP/1.1 removed.
Host: D
In this example, the server uses a URI # Request
Template for the client to specify a POST /parts/merge?src=part/
# Response
HTTP/1.1 201 Created category for the resource to be moved to: XYZ&dest=part/ABCHTTP/1.1
Content-Type:application/xml; Host: www.service.com
Location:www.service.com/parts
# Request
<parts>
POST /parts/engine/XYZ/move;t=<token>? When to use Link Relation Types
<link rel=”self” href=”
www.service.com /parts/ group=Jeep HTTP/1.1 A link relation type describes the role or
engine”/>
Host: www.service.com purpose of a link. Links are not useful if
...
correct semantics are not assigned to
</parts>
Consider merging resources when the them. All relation type values are case
A moving operation is used when one client would like to merge two or more insensitive. Multiple values for each
resource needs to be moved to some resources presented to the server. relation can be considered.
Access Resources
Resource
Servers
Access REST Service
Iss
ue
Tok
en
Grant Access
Authorization
Server
Now, to retrieve version 2 of the same Expires Date and time when representation will expire. (HTTP 1.0 clients)
resource in JSON format: Age Total time in seconds since the representation was retrieved from the server.
Application integration using REST and a prefect use case for designing
RESTful web services in the right manner
These days, REST is used everywhere – Account Application, and presents the • getAllAccounts
from desktops to mobiles and even in the number of operations that are possible • billPayment
likes of Facebook, Google, and Amazon. while working with a banking application. • cancelPayment
REST provides a lighter-weight alternative • completePayment
for application integration. The REST • createAccountHolderProfile • fundTransfer
architecture allows working in a variety of • getAccountHolderProfile • addPayee
scenarios and it is very useful in cloud and • updateAccountHolderProfile • updatePayee
mobile development. • doLogin • deletePayee
• doLogOut • createFixedDeposit
Here, a real-time example is provided for
• getAccountSummary • preCloserFixedDeposit
creating RESTful web service for a complex
• getLoanAccounts
system. This example is about Banking
The second step would be to design the URLs, mapped with the business operations:
• Account
• Profile
• Bill
• Loan
• Payee
• Fund
• Fixed Deposit
• Payment
As all browsers support JSON, and it is very
lightweight, we are going to use it as a
representation format.
Response/JSON
Payment DB
Response/JSON
Profile DB
HTTP PUT Request URL3
Response/JSON
Banking Application
Deepak Kumar is the Senior Technology Architect with Infosys Digital group. He has 11+ years of IT industry
experience in Software Design and Development. He has good hands-on experience in designing SharePoint™,
Microsoft .NET based CMS, .NET, and J2EE applications. His strength lies in the ability to handle Multi-tier
Application Design, Content Management, Internet / UI based applications, Object Oriented Design, and
Component Based Design.
You can reach out to him at deepak_kumar14@infosys.com
© 2017 Infosys Limited, Bengaluru, India. All Rights Reserved. Infosys believes the information in this document is accurate as of its publication date; such information is subject to change without notice. Infosys
acknowledges the proprietary rights of other companies to the trademarks, product names and such other intellectual property rights mentioned in this document. Except as expressly permitted, neither this
documentation nor any part of it may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, printing, photocopying, recording or otherwise, without the
prior permission of Infosys Limited and/ or any named intellectual property rights holders under this document.