SlideShare a Scribd company logo
REST API basics
Traditional web applications
Client Server
GET /the-resource
...
200 OK
<html>Code...</html>
Displays the page,
then user clicks
on link.
GET /another-resource
...
200 OK
<html>Code...</html>
Displays the other
page, ...
Traditional web applications
The interface is built on HTML & HTTP.
• Drawbacks:
• The client must understand both HTTP and HTML.
• The entire webpage is replaced with another one.
• No way to animate transitions between webpages.
• Same data is usually sent in multiple responses.
• E.g. HTML code for the layout.
Traditional web applications
Client Server
HTTP &
HTML
Client
???
• HTTP & HTML can be used, but is not optimal.
• The GUI on smartphones does not use HTML.
• E.g. GET /users/3:
<h1>Claire</h1>
<p>Claire is 24 years old and lives in Boston.</p>
Name
Age City
Application programming interface
An API is an interface for Machine ↔ Machine communication.
• An API making use of HTTP is called a Web API.
A GUI is an interface for Human ↔ Machine communication.
Server
Client
API GUI
User
Different types ofWeb APIs
• Remote Procedure Call, RPC.
• Clients can call functions on the server.
• Remote Method Invocation, RMI.
• Clients can call methods on objects on the server.
• Representational StateTransfer, REST.
• Clients can applyCRUD operations on resources on the server.
What is REST?
An architectural style for distributed hypermedia systems described by Roy
Thomas Fielding in his doctoral dissertation 2000.
• Consists of constraints:
1. Client - Server
2. Stateless
3. Cache
4. Uniform Interface
5. Layered System
6. Code-On-Demand Client Server Server
Relational
Database
Web
Application
Web
Browser HTTP SQL
What does REST mean?
The name "Representational StateTransfer" is intended to evoke an image of how a
well-designedWeb application behaves: a network of web pages (a virtual state-
machine), where the user progresses through the application by selecting links (state
transitions), resulting in the next page (representing the next state of the application)
being transferred to the user and rendered for their use.
From Roy's dissertation.
What does REST mean?
Server
Id Name
1 Alice
2 Bob
3 Claire
Users
Client GET /users/2
...
{"id": 2, "name": "Bob"}
Changes state.
{"id": 2,
"name": "Obi"}
PUT /users/2
{"id": 2, "name": "Obi"}
Using HTTP as the uniform interface
• Use URIs to identify resources.
• Use HTTP methods to specify operation:
• Create: POST (or PUT)
• Retrieve:GET
• Update: PUT (or PATCH)
• Delete: DELETE
• Use HTTP headers
Content-Type and Accept
to specify data format for the resources.
• Use HTTP status code to indicate success/failure.
Bad
POST /login
POST /create-book
GET /get-top-10-books
Good
POST /login-sessions
POST /books
GET /top-10-books
Using HTTP as the uniform interface
REST is an architectural style, not a specification.
• In practice, it can be used in many different ways.
• But some are better than others.
Good recommendations:
• Web API Design - Crafting Interfaces that Developers Love
• https://pages.apigee.com/rs/apigee/images/api-design-ebook-2012-03.pdf
REST example
A server with information about users.
• The GET method is used to retrieve resources.
• GET /users
• GET /users/2
• GET /users/pages/1
• GET /users/gender/female
• GET /users/age/18
• GET /users/???
• GET /users/2/name
• GET /users/2/pets
GET /users?page=1
GET /users?gender=female
GET /users?age=18
GET /users?gender=female&age=18
Id Name
1 Alice
2 Bob
3 Claire
Users
REST example
A server with information about users.
• The GET method is used to retrieve resources.
• Which data format? Specified by the Accept header!
GET /users HTTP/1.1
Host: the-website.com
Accept: application/json
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 66
[
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"}
]
application/xml
was popular before
JSON.
Id Name
1 Alice
2 Bob
3 Claire
Users
REST example
A server with information about users.
• The POST method is used to create resources.
• Which data format? Specified by the Accept and Content-Type header!
POST /users HTTP/1.1
Host: the-website.com
Accept: application/json
Content-Type: application/xml
Content-Length: 49
<user>
<name>Claire</name>
</user>
HTTP/1.1 201 Created
Location: /users/3
Content-Type: application/json
Content-Length: 28
{"id": 3, "name": "Claire"}
Id Name
1 Alice
2 Bob
3 Claire
Users
REST example
A server with information about users.
• The PUT method is used to update an entire resource.
PUT /users/3 HTTP/1.1
Host: the-website.com
Content-Type: application/xml
Content-Length: 52
<user>
<id>3</id>
<name>Cecilia</name>
</user>
HTTP/1.1 204 No Content
PUT can also be used to
create a resource if you
know which URI it should
have in advance.
Id Name
1 Alice
2 Bob
3 Claire
Users
REST example
A server with information about users.
• The DELETE method is used to delete a resource.
DELETE /users/2 HTTP/1.1
Host: the-website.com
HTTP/1.1 204 No Content
Id Name
1 Alice
2 Bob
3 Claire
Users
REST example
A server with information about users.
• The PATCH method is used to update parts of a resource.
PATCH /users/1 HTTP/1.1
Host: the-website.com
Content-Type: application/xml
Content-Length: 37
<user>
<name>Amanda</human>
</user>
HTTP/1.1 204 No Content
The PATCH method
is only a proposed
standard.
Id Name
1 Alice
2 Bob
3 Claire
Users
REST example
A server with information about users.
• What if something goes wrong?
• Use the HTTP status codes to indicate success/failure.
GET /users/999 HTTP/1.1
Host: the-website.com
Accept: application/json
HTTP/1.1 404 Not Found
• Read more about the different status codes at:
• http://www.restapitutorial.com/httpstatuscodes.html
• Optionally include error messages in the response body.
Id Name
1 Alice
2 Bob
3 Claire
Users
Designing a REST api
How should you think?
• Make it as easy as possible to use by other programmers.
Facebook:
• Always return 200 OK.
• GET /v2.7/{user-id}
• GET /v2.7/{post-id}
• GET /v2.7/{user-id}/friends
• GET /v2.7/{object-id}/likes
Designing a REST api
How should you think?
• Make it as easy as possible to use by other programmers.
Twitter:
• Only use GET and POST.
• GET /1.1/users/show.json?user_id=2244994945
• POST /1.1/favorites/destroy.json?id=243138128959913986

More Related Content

PPTX
rest-api-basics.pptx
PDF
REST APIS web development for backend familiarity
PPTX
rest-api-basics.pptx
PPTX
Rest APIs Training
PPTX
RESTful APIs
PDF
What is REST?
PDF
RESTful applications: The why and how by Maikel Mardjan
PPTX
REST API
rest-api-basics.pptx
REST APIS web development for backend familiarity
rest-api-basics.pptx
Rest APIs Training
RESTful APIs
What is REST?
RESTful applications: The why and how by Maikel Mardjan
REST API

Similar to Tutorial_Rest_API_For_Beginners_125.pptx (20)

PPTX
Http and REST APIs.
PPTX
REST and RESTful Services
PPT
emilio.ppt
PPT
emilio.ppt
PPTX
RESTful Services
PDF
WebApp #3 : API
PDF
RESTful web
PPTX
RESTful Web Services
PDF
Rest API Interview Questions PDF By ScholarHat
PPTX
Standards of rest api
PDF
Introduction to REST - REST Basics - JSON
PPTX
REST & RESTful Web Services
PPTX
RESTful APIs in .NET
PPTX
Rest WebAPI with OData
PPTX
REST API
PPTX
Rest Webservice
PDF
REST Api with Asp Core
PDF
Creating Restful Web Services with restish
PPTX
80068
PPTX
REST Methodologies
Http and REST APIs.
REST and RESTful Services
emilio.ppt
emilio.ppt
RESTful Services
WebApp #3 : API
RESTful web
RESTful Web Services
Rest API Interview Questions PDF By ScholarHat
Standards of rest api
Introduction to REST - REST Basics - JSON
REST & RESTful Web Services
RESTful APIs in .NET
Rest WebAPI with OData
REST API
Rest Webservice
REST Api with Asp Core
Creating Restful Web Services with restish
80068
REST Methodologies
Ad

Recently uploaded (20)

PDF
Insiders guide to clinical Medicine.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Cell Structure & Organelles in detailed.
PDF
Pre independence Education in Inndia.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PPTX
Pharma ospi slides which help in ospi learning
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Business Ethics Teaching Materials for college
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
Insiders guide to clinical Medicine.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Cell Structure & Organelles in detailed.
Pre independence Education in Inndia.pdf
TR - Agricultural Crops Production NC III.pdf
Microbial disease of the cardiovascular and lymphatic systems
The Final Stretch: How to Release a Game and Not Die in the Process.
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Open Quiz Monsoon Mind Game Final Set.pptx
Pharma ospi slides which help in ospi learning
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
GDM (1) (1).pptx small presentation for students
human mycosis Human fungal infections are called human mycosis..pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Business Ethics Teaching Materials for college
Open Quiz Monsoon Mind Game Prelims.pptx
Ad

Tutorial_Rest_API_For_Beginners_125.pptx

  • 2. Traditional web applications Client Server GET /the-resource ... 200 OK <html>Code...</html> Displays the page, then user clicks on link. GET /another-resource ... 200 OK <html>Code...</html> Displays the other page, ...
  • 3. Traditional web applications The interface is built on HTML & HTTP. • Drawbacks: • The client must understand both HTTP and HTML. • The entire webpage is replaced with another one. • No way to animate transitions between webpages. • Same data is usually sent in multiple responses. • E.g. HTML code for the layout.
  • 4. Traditional web applications Client Server HTTP & HTML Client ??? • HTTP & HTML can be used, but is not optimal. • The GUI on smartphones does not use HTML. • E.g. GET /users/3: <h1>Claire</h1> <p>Claire is 24 years old and lives in Boston.</p> Name Age City
  • 5. Application programming interface An API is an interface for Machine ↔ Machine communication. • An API making use of HTTP is called a Web API. A GUI is an interface for Human ↔ Machine communication. Server Client API GUI User
  • 6. Different types ofWeb APIs • Remote Procedure Call, RPC. • Clients can call functions on the server. • Remote Method Invocation, RMI. • Clients can call methods on objects on the server. • Representational StateTransfer, REST. • Clients can applyCRUD operations on resources on the server.
  • 7. What is REST? An architectural style for distributed hypermedia systems described by Roy Thomas Fielding in his doctoral dissertation 2000. • Consists of constraints: 1. Client - Server 2. Stateless 3. Cache 4. Uniform Interface 5. Layered System 6. Code-On-Demand Client Server Server Relational Database Web Application Web Browser HTTP SQL
  • 8. What does REST mean? The name "Representational StateTransfer" is intended to evoke an image of how a well-designedWeb application behaves: a network of web pages (a virtual state- machine), where the user progresses through the application by selecting links (state transitions), resulting in the next page (representing the next state of the application) being transferred to the user and rendered for their use. From Roy's dissertation.
  • 9. What does REST mean? Server Id Name 1 Alice 2 Bob 3 Claire Users Client GET /users/2 ... {"id": 2, "name": "Bob"} Changes state. {"id": 2, "name": "Obi"} PUT /users/2 {"id": 2, "name": "Obi"}
  • 10. Using HTTP as the uniform interface • Use URIs to identify resources. • Use HTTP methods to specify operation: • Create: POST (or PUT) • Retrieve:GET • Update: PUT (or PATCH) • Delete: DELETE • Use HTTP headers Content-Type and Accept to specify data format for the resources. • Use HTTP status code to indicate success/failure. Bad POST /login POST /create-book GET /get-top-10-books Good POST /login-sessions POST /books GET /top-10-books
  • 11. Using HTTP as the uniform interface REST is an architectural style, not a specification. • In practice, it can be used in many different ways. • But some are better than others. Good recommendations: • Web API Design - Crafting Interfaces that Developers Love • https://pages.apigee.com/rs/apigee/images/api-design-ebook-2012-03.pdf
  • 12. REST example A server with information about users. • The GET method is used to retrieve resources. • GET /users • GET /users/2 • GET /users/pages/1 • GET /users/gender/female • GET /users/age/18 • GET /users/??? • GET /users/2/name • GET /users/2/pets GET /users?page=1 GET /users?gender=female GET /users?age=18 GET /users?gender=female&age=18 Id Name 1 Alice 2 Bob 3 Claire Users
  • 13. REST example A server with information about users. • The GET method is used to retrieve resources. • Which data format? Specified by the Accept header! GET /users HTTP/1.1 Host: the-website.com Accept: application/json HTTP/1.1 200 OK Content-Type: application/json Content-Length: 66 [ {"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"} ] application/xml was popular before JSON. Id Name 1 Alice 2 Bob 3 Claire Users
  • 14. REST example A server with information about users. • The POST method is used to create resources. • Which data format? Specified by the Accept and Content-Type header! POST /users HTTP/1.1 Host: the-website.com Accept: application/json Content-Type: application/xml Content-Length: 49 <user> <name>Claire</name> </user> HTTP/1.1 201 Created Location: /users/3 Content-Type: application/json Content-Length: 28 {"id": 3, "name": "Claire"} Id Name 1 Alice 2 Bob 3 Claire Users
  • 15. REST example A server with information about users. • The PUT method is used to update an entire resource. PUT /users/3 HTTP/1.1 Host: the-website.com Content-Type: application/xml Content-Length: 52 <user> <id>3</id> <name>Cecilia</name> </user> HTTP/1.1 204 No Content PUT can also be used to create a resource if you know which URI it should have in advance. Id Name 1 Alice 2 Bob 3 Claire Users
  • 16. REST example A server with information about users. • The DELETE method is used to delete a resource. DELETE /users/2 HTTP/1.1 Host: the-website.com HTTP/1.1 204 No Content Id Name 1 Alice 2 Bob 3 Claire Users
  • 17. REST example A server with information about users. • The PATCH method is used to update parts of a resource. PATCH /users/1 HTTP/1.1 Host: the-website.com Content-Type: application/xml Content-Length: 37 <user> <name>Amanda</human> </user> HTTP/1.1 204 No Content The PATCH method is only a proposed standard. Id Name 1 Alice 2 Bob 3 Claire Users
  • 18. REST example A server with information about users. • What if something goes wrong? • Use the HTTP status codes to indicate success/failure. GET /users/999 HTTP/1.1 Host: the-website.com Accept: application/json HTTP/1.1 404 Not Found • Read more about the different status codes at: • http://www.restapitutorial.com/httpstatuscodes.html • Optionally include error messages in the response body. Id Name 1 Alice 2 Bob 3 Claire Users
  • 19. Designing a REST api How should you think? • Make it as easy as possible to use by other programmers. Facebook: • Always return 200 OK. • GET /v2.7/{user-id} • GET /v2.7/{post-id} • GET /v2.7/{user-id}/friends • GET /v2.7/{object-id}/likes
  • 20. Designing a REST api How should you think? • Make it as easy as possible to use by other programmers. Twitter: • Only use GET and POST. • GET /1.1/users/show.json?user_id=2244994945 • POST /1.1/favorites/destroy.json?id=243138128959913986