REST API
By Devi Kiran
KickStartPros.com
contact@kickstartpros.com
• JSON1
• REST API2
• JS libs and Play3
Agenda
JSON
• JSON = JavaScript Object Notation
• Lightweight data interchange format
• Organization with Employees
REST API & DESIGN
What is REST?
• REST = REpresentational State Transfer
• Architectural Constraints
– Client-Server
– Uniform Interface
– Stateless
– Cacheable
– Layered System
– Code on demand
REST - Resource Based
REST
• Nouns, Not Verbs
• Things are identified by
URIs
• Person, User, Address
resource
• HTTP Verb to dictate the
operation on that resource.
• Multiple URIs pointing to
same resource.
SOAP - RPC
• Verbs or Actions.
• RPC calls (Don’t do this in
REST)
– getUserData
– getAllUsers
– searchUsers
– getUserAddress
– updateUserAddress
– deleteUser
Representations
• Represent part of the
resource state.
• JSON or XML
• Example: Buy a car
– Resource: Car (Audi A1)
– Service: Buy (POST)
– Representation:
• Name, Model, Price
• JSON or XML format.
REST API Design
Different REST Clients
iPhone App
Browser/
Web
Android App DELETE
PUT
GET
POST
CUSTOM
Params
Http
Methods
Your App Server
Load balanced
App
Server 1
App
Server
App
Server
App
Server
1…n
KickStartPros.com
Http/s
JSON/XML
JSON/XML
JSON/XML
HTTP Methods & Status Codes
• GET
• HEAD
• POST
• PUT
• DELETE
• TRACE
• OPTIONS
• 2xx Success
– 200 Ok
– 204 No Content
• 3xx Redirect
– 304 Not Modified
• 4xx Client Error
– 400 Bad Request
– 401 Unauthorized
– 403 Forbidden
– 404 Not Found
• 5xx Server Error
– 500 Internal Server Error
Keep Http/s Simple
• Resources – URI format
– Collection Resources (/users)
– Instance Resources (/users/ayl23d)
• Behavior
– Use Http Methods => CURD Database/Server Actions
– POST => Create
– GET => Read
– PUT => Update
– DELETE => Delete
– HEAD => Headers, no body
• Media types
– application/json
– application/xml
Post for Create
POST /users
{
“name” : “Kiran”
}
Response:
201 Created
Location:
http://www.kickstartpros.com/users/gdk23
Put for Update
PUT /users/<userId>
PUT /users/gdk23
{
“name” : “Devi Kiran”
“description”: “Trainer”
}
Post for Update
POST /users/<userId>
POST /users/gdk23
{
“name”: “Devi Kiran”
}
(Partial update or Full update)
Response:
200 OK
ARCHITECTURAL
CONSTRAINTS
Client and Server
• Uniform Interface Decouples Client and
Server
• Design – http and URIs
– HTTP verbs (GET, PUT, POST, DELETE)
– URIs
– HTTP response
• Status
• Body
Uniform interface
• The uniform interface decouples client and
Server
– Identification of resources
– Manipulation of resources through these
representations
– Self-descriptive messages
HATEOAS
• Hypermedia as the engine of application state
• Client to Server
– Body Content
– URI and Query String parameters
– Request Headers
• Server
– Body Content
– Response Codes
– Response Headers
Stateless
• No client state at server.
• Any State is maintained
at Client side.
• Each request has all the
information to process
the request.
Client
1
Client
2
Client
3
Servers
More …
• Layered System
– A client cannot tell
whether it is connected
directly to the server.
– Load balanced Servers
– More Performance
• Cacheable
– Implicit
– Explicit
REST APIS & PLAY
Example URIs
• GMail Rest API (v1 Reference by resource
type)
• Custom REST API from Yahoo
– https://developer.yahoo.com/yql/console/
– Example: get san francisco geo data:
select * from geo.places where text=“san francisco,
ca”
UI Data binding of REST API
• JQuery – Ajax calls
• Data Binding JS libs that help to bind with REST
response
– JQuery UI – Widgets
– Knockout – Model-View-ViewModel (MVVM)
– Angular JS – MVVM & MVC
– Backbone.js – RESTful JSON interface
– Ember.js – Template language, MVC and a router
– Kendo UI – MVVM
– Extjs – MVC (Model View Controller) & widgets
Summary & Reference
• JSON & REST Architecture
• Resource based REST API = Micro Services (SOA)
• REST API & Examples with JS libs
• Reference – Wiki
• contact@kickstartpros.com
– Join our trainings.
– Kick Start your project with us.

REST API Design

  • 1.
    REST API By DeviKiran KickStartPros.com contact@kickstartpros.com
  • 2.
    • JSON1 • RESTAPI2 • JS libs and Play3 Agenda
  • 3.
    JSON • JSON =JavaScript Object Notation • Lightweight data interchange format • Organization with Employees
  • 4.
    REST API &DESIGN
  • 5.
    What is REST? •REST = REpresentational State Transfer • Architectural Constraints – Client-Server – Uniform Interface – Stateless – Cacheable – Layered System – Code on demand
  • 6.
    REST - ResourceBased REST • Nouns, Not Verbs • Things are identified by URIs • Person, User, Address resource • HTTP Verb to dictate the operation on that resource. • Multiple URIs pointing to same resource. SOAP - RPC • Verbs or Actions. • RPC calls (Don’t do this in REST) – getUserData – getAllUsers – searchUsers – getUserAddress – updateUserAddress – deleteUser
  • 7.
    Representations • Represent partof the resource state. • JSON or XML • Example: Buy a car – Resource: Car (Audi A1) – Service: Buy (POST) – Representation: • Name, Model, Price • JSON or XML format.
  • 8.
    REST API Design DifferentREST Clients iPhone App Browser/ Web Android App DELETE PUT GET POST CUSTOM Params Http Methods Your App Server Load balanced App Server 1 App Server App Server App Server 1…n KickStartPros.com Http/s JSON/XML JSON/XML JSON/XML
  • 9.
    HTTP Methods &Status Codes • GET • HEAD • POST • PUT • DELETE • TRACE • OPTIONS • 2xx Success – 200 Ok – 204 No Content • 3xx Redirect – 304 Not Modified • 4xx Client Error – 400 Bad Request – 401 Unauthorized – 403 Forbidden – 404 Not Found • 5xx Server Error – 500 Internal Server Error
  • 10.
    Keep Http/s Simple •Resources – URI format – Collection Resources (/users) – Instance Resources (/users/ayl23d) • Behavior – Use Http Methods => CURD Database/Server Actions – POST => Create – GET => Read – PUT => Update – DELETE => Delete – HEAD => Headers, no body • Media types – application/json – application/xml
  • 11.
    Post for Create POST/users { “name” : “Kiran” } Response: 201 Created Location: http://www.kickstartpros.com/users/gdk23
  • 12.
    Put for Update PUT/users/<userId> PUT /users/gdk23 { “name” : “Devi Kiran” “description”: “Trainer” }
  • 13.
    Post for Update POST/users/<userId> POST /users/gdk23 { “name”: “Devi Kiran” } (Partial update or Full update) Response: 200 OK
  • 14.
  • 15.
    Client and Server •Uniform Interface Decouples Client and Server • Design – http and URIs – HTTP verbs (GET, PUT, POST, DELETE) – URIs – HTTP response • Status • Body
  • 16.
    Uniform interface • Theuniform interface decouples client and Server – Identification of resources – Manipulation of resources through these representations – Self-descriptive messages
  • 17.
    HATEOAS • Hypermedia asthe engine of application state • Client to Server – Body Content – URI and Query String parameters – Request Headers • Server – Body Content – Response Codes – Response Headers
  • 18.
    Stateless • No clientstate at server. • Any State is maintained at Client side. • Each request has all the information to process the request. Client 1 Client 2 Client 3 Servers
  • 19.
    More … • LayeredSystem – A client cannot tell whether it is connected directly to the server. – Load balanced Servers – More Performance • Cacheable – Implicit – Explicit
  • 20.
  • 21.
    Example URIs • GMailRest API (v1 Reference by resource type) • Custom REST API from Yahoo – https://developer.yahoo.com/yql/console/ – Example: get san francisco geo data: select * from geo.places where text=“san francisco, ca”
  • 22.
    UI Data bindingof REST API • JQuery – Ajax calls • Data Binding JS libs that help to bind with REST response – JQuery UI – Widgets – Knockout – Model-View-ViewModel (MVVM) – Angular JS – MVVM & MVC – Backbone.js – RESTful JSON interface – Ember.js – Template language, MVC and a router – Kendo UI – MVVM – Extjs – MVC (Model View Controller) & widgets
  • 23.
    Summary & Reference •JSON & REST Architecture • Resource based REST API = Micro Services (SOA) • REST API & Examples with JS libs • Reference – Wiki • contact@kickstartpros.com – Join our trainings. – Kick Start your project with us.