SlideShare a Scribd company logo
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"}
Most read
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
Most read
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
Most read
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 OF WEB 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 State Transfer, REST.
• Clients can apply CRUD 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
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

Similar to rest-api-basics.pptx (20)

emilio.ppt
emilio.pptemilio.ppt
emilio.ppt
Mohit Joshi
 
emilio.ppt
emilio.pptemilio.ppt
emilio.ppt
DeepakKumar772882
 
REST & RESTful Web Services
REST & RESTful Web ServicesREST & RESTful Web Services
REST & RESTful Web Services
Halil Burak Cetinkaya
 
REST Api with Asp Core
REST Api with Asp CoreREST Api with Asp Core
REST Api with Asp Core
Irina Scurtu
 
RESTful Services
RESTful ServicesRESTful Services
RESTful Services
Jason Gerard
 
Rest Webservice
Rest WebserviceRest Webservice
Rest Webservice
Viyaan Jhiingade
 
REST and RESTful Services
REST and RESTful ServicesREST and RESTful Services
REST and RESTful Services
Damian T. Gordon
 
RESTful web
RESTful webRESTful web
RESTful web
Alvin Qi
 
RESTful applications: The why and how by Maikel Mardjan
RESTful applications: The why and how by Maikel MardjanRESTful applications: The why and how by Maikel Mardjan
RESTful applications: The why and how by Maikel Mardjan
Jexia
 
Rest WebAPI with OData
Rest WebAPI with ODataRest WebAPI with OData
Rest WebAPI with OData
Mahek Merchant
 
REST Methodologies
REST MethodologiesREST Methodologies
REST Methodologies
jrodbx
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
adeppathondur
 
Creating Restful Web Services with restish
Creating Restful Web Services with restishCreating Restful Web Services with restish
Creating Restful Web Services with restish
Grig Gheorghiu
 
Standards of rest api
Standards of rest apiStandards of rest api
Standards of rest api
Maýur Chourasiya
 
RESTful SOA - 中科院暑期讲座
RESTful SOA - 中科院暑期讲座RESTful SOA - 中科院暑期讲座
RESTful SOA - 中科院暑期讲座
Li Yi
 
Restful web-services
Restful web-servicesRestful web-services
Restful web-services
rporwal
 
Introduction to REST - REST Basics - JSON
Introduction to REST - REST Basics - JSONIntroduction to REST - REST Basics - JSON
Introduction to REST - REST Basics - JSON
Matrix823409
 
Real world RESTful service development problems and solutions
Real world RESTful service development problems and solutionsReal world RESTful service development problems and solutions
Real world RESTful service development problems and solutions
Bhakti Mehta
 
Building Restful Applications Using Php
Building Restful Applications Using PhpBuilding Restful Applications Using Php
Building Restful Applications Using Php
Sudheer Satyanarayana
 
RESTful APIs in .NET
RESTful APIs in .NETRESTful APIs in .NET
RESTful APIs in .NET
Greg Sohl
 
REST Api with Asp Core
REST Api with Asp CoreREST Api with Asp Core
REST Api with Asp Core
Irina Scurtu
 
RESTful web
RESTful webRESTful web
RESTful web
Alvin Qi
 
RESTful applications: The why and how by Maikel Mardjan
RESTful applications: The why and how by Maikel MardjanRESTful applications: The why and how by Maikel Mardjan
RESTful applications: The why and how by Maikel Mardjan
Jexia
 
Rest WebAPI with OData
Rest WebAPI with ODataRest WebAPI with OData
Rest WebAPI with OData
Mahek Merchant
 
REST Methodologies
REST MethodologiesREST Methodologies
REST Methodologies
jrodbx
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
adeppathondur
 
Creating Restful Web Services with restish
Creating Restful Web Services with restishCreating Restful Web Services with restish
Creating Restful Web Services with restish
Grig Gheorghiu
 
RESTful SOA - 中科院暑期讲座
RESTful SOA - 中科院暑期讲座RESTful SOA - 中科院暑期讲座
RESTful SOA - 中科院暑期讲座
Li Yi
 
Restful web-services
Restful web-servicesRestful web-services
Restful web-services
rporwal
 
Introduction to REST - REST Basics - JSON
Introduction to REST - REST Basics - JSONIntroduction to REST - REST Basics - JSON
Introduction to REST - REST Basics - JSON
Matrix823409
 
Real world RESTful service development problems and solutions
Real world RESTful service development problems and solutionsReal world RESTful service development problems and solutions
Real world RESTful service development problems and solutions
Bhakti Mehta
 
Building Restful Applications Using Php
Building Restful Applications Using PhpBuilding Restful Applications Using Php
Building Restful Applications Using Php
Sudheer Satyanarayana
 
RESTful APIs in .NET
RESTful APIs in .NETRESTful APIs in .NET
RESTful APIs in .NET
Greg Sohl
 

Recently uploaded (17)

3D Graphics an introduction and details .pptx
3D Graphics an introduction and details .pptx3D Graphics an introduction and details .pptx
3D Graphics an introduction and details .pptx
islamicknowledge5224
 
10 Latest Technologies and Their Benefits to End.pptx
10 Latest Technologies and Their Benefits to End.pptx10 Latest Technologies and Their Benefits to End.pptx
10 Latest Technologies and Their Benefits to End.pptx
EphraimOOghodero
 
最新版西班牙加泰罗尼亚国际大学毕业证(UIC毕业证书)原版定制
最新版西班牙加泰罗尼亚国际大学毕业证(UIC毕业证书)原版定制最新版西班牙加泰罗尼亚国际大学毕业证(UIC毕业证书)原版定制
最新版西班牙加泰罗尼亚国际大学毕业证(UIC毕业证书)原版定制
Taqyea
 
Darley - BSides Nairobi (2025-06-07) Epochalypse 2038 - Time is Not on Our Si...
Darley - BSides Nairobi (2025-06-07) Epochalypse 2038 - Time is Not on Our Si...Darley - BSides Nairobi (2025-06-07) Epochalypse 2038 - Time is Not on Our Si...
Darley - BSides Nairobi (2025-06-07) Epochalypse 2038 - Time is Not on Our Si...
treyka
 
Vigilanti-Cura-Protecting-the-Faith.pptx
Vigilanti-Cura-Protecting-the-Faith.pptxVigilanti-Cura-Protecting-the-Faith.pptx
Vigilanti-Cura-Protecting-the-Faith.pptx
secretarysocom
 
ICP -2 Review – What It Is, and How to Participate and Provide Your Feedback
ICP -2 Review – What It Is, and How to Participate and Provide Your FeedbackICP -2 Review – What It Is, and How to Participate and Provide Your Feedback
ICP -2 Review – What It Is, and How to Participate and Provide Your Feedback
APNIC
 
What to Expect When Hiring Shopify Development Services_ A Technical Walkthro...
What to Expect When Hiring Shopify Development Services_ A Technical Walkthro...What to Expect When Hiring Shopify Development Services_ A Technical Walkthro...
What to Expect When Hiring Shopify Development Services_ A Technical Walkthro...
CartCoders
 
MOBILE PHONE DATA presentation with all necessary details
MOBILE PHONE DATA presentation with all necessary detailsMOBILE PHONE DATA presentation with all necessary details
MOBILE PHONE DATA presentation with all necessary details
benamorraj
 
Predicting Site Quality Google Patent US9767157B2 - Behzad Hussain.pdf
Predicting Site Quality Google Patent US9767157B2 - Behzad Hussain.pdfPredicting Site Quality Google Patent US9767157B2 - Behzad Hussain.pdf
Predicting Site Quality Google Patent US9767157B2 - Behzad Hussain.pdf
Behzad Hussain
 
Inter-Mirifica-Navigating-Media-in-the-Modern-World.pptx
Inter-Mirifica-Navigating-Media-in-the-Modern-World.pptxInter-Mirifica-Navigating-Media-in-the-Modern-World.pptx
Inter-Mirifica-Navigating-Media-in-the-Modern-World.pptx
secretarysocom
 
UV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animation
UV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animationUV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animation
UV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animation
17218
 
Cloud Computing - iCloud by Hamza Anwaar .pptx
Cloud Computing - iCloud by Hamza Anwaar .pptxCloud Computing - iCloud by Hamza Anwaar .pptx
Cloud Computing - iCloud by Hamza Anwaar .pptx
islamicknowledge5224
 
Networking_Essentials_version_3.0_-_Module_7.pptx
Networking_Essentials_version_3.0_-_Module_7.pptxNetworking_Essentials_version_3.0_-_Module_7.pptx
Networking_Essentials_version_3.0_-_Module_7.pptx
elestirmen
 
ARTIFICIAL INTELLIGENCE.pptx2565567765676
ARTIFICIAL INTELLIGENCE.pptx2565567765676ARTIFICIAL INTELLIGENCE.pptx2565567765676
ARTIFICIAL INTELLIGENCE.pptx2565567765676
areebaimtiazpmas
 
Internet_of_Things_Presentation_by-Humera.pptx
Internet_of_Things_Presentation_by-Humera.pptxInternet_of_Things_Presentation_by-Humera.pptx
Internet_of_Things_Presentation_by-Humera.pptx
cshumerabashir
 
How to Make Money as a Cam Model – Tips, Tools & Real Talk
How to Make Money as a Cam Model – Tips, Tools & Real TalkHow to Make Money as a Cam Model – Tips, Tools & Real Talk
How to Make Money as a Cam Model – Tips, Tools & Real Talk
Cam Sites Expert
 
Google_Cloud_Computing_Fundamentals.pptx
Google_Cloud_Computing_Fundamentals.pptxGoogle_Cloud_Computing_Fundamentals.pptx
Google_Cloud_Computing_Fundamentals.pptx
ektadangwal2005
 
3D Graphics an introduction and details .pptx
3D Graphics an introduction and details .pptx3D Graphics an introduction and details .pptx
3D Graphics an introduction and details .pptx
islamicknowledge5224
 
10 Latest Technologies and Their Benefits to End.pptx
10 Latest Technologies and Their Benefits to End.pptx10 Latest Technologies and Their Benefits to End.pptx
10 Latest Technologies and Their Benefits to End.pptx
EphraimOOghodero
 
最新版西班牙加泰罗尼亚国际大学毕业证(UIC毕业证书)原版定制
最新版西班牙加泰罗尼亚国际大学毕业证(UIC毕业证书)原版定制最新版西班牙加泰罗尼亚国际大学毕业证(UIC毕业证书)原版定制
最新版西班牙加泰罗尼亚国际大学毕业证(UIC毕业证书)原版定制
Taqyea
 
Darley - BSides Nairobi (2025-06-07) Epochalypse 2038 - Time is Not on Our Si...
Darley - BSides Nairobi (2025-06-07) Epochalypse 2038 - Time is Not on Our Si...Darley - BSides Nairobi (2025-06-07) Epochalypse 2038 - Time is Not on Our Si...
Darley - BSides Nairobi (2025-06-07) Epochalypse 2038 - Time is Not on Our Si...
treyka
 
Vigilanti-Cura-Protecting-the-Faith.pptx
Vigilanti-Cura-Protecting-the-Faith.pptxVigilanti-Cura-Protecting-the-Faith.pptx
Vigilanti-Cura-Protecting-the-Faith.pptx
secretarysocom
 
ICP -2 Review – What It Is, and How to Participate and Provide Your Feedback
ICP -2 Review – What It Is, and How to Participate and Provide Your FeedbackICP -2 Review – What It Is, and How to Participate and Provide Your Feedback
ICP -2 Review – What It Is, and How to Participate and Provide Your Feedback
APNIC
 
What to Expect When Hiring Shopify Development Services_ A Technical Walkthro...
What to Expect When Hiring Shopify Development Services_ A Technical Walkthro...What to Expect When Hiring Shopify Development Services_ A Technical Walkthro...
What to Expect When Hiring Shopify Development Services_ A Technical Walkthro...
CartCoders
 
MOBILE PHONE DATA presentation with all necessary details
MOBILE PHONE DATA presentation with all necessary detailsMOBILE PHONE DATA presentation with all necessary details
MOBILE PHONE DATA presentation with all necessary details
benamorraj
 
Predicting Site Quality Google Patent US9767157B2 - Behzad Hussain.pdf
Predicting Site Quality Google Patent US9767157B2 - Behzad Hussain.pdfPredicting Site Quality Google Patent US9767157B2 - Behzad Hussain.pdf
Predicting Site Quality Google Patent US9767157B2 - Behzad Hussain.pdf
Behzad Hussain
 
Inter-Mirifica-Navigating-Media-in-the-Modern-World.pptx
Inter-Mirifica-Navigating-Media-in-the-Modern-World.pptxInter-Mirifica-Navigating-Media-in-the-Modern-World.pptx
Inter-Mirifica-Navigating-Media-in-the-Modern-World.pptx
secretarysocom
 
UV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animation
UV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animationUV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animation
UV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animation
17218
 
Cloud Computing - iCloud by Hamza Anwaar .pptx
Cloud Computing - iCloud by Hamza Anwaar .pptxCloud Computing - iCloud by Hamza Anwaar .pptx
Cloud Computing - iCloud by Hamza Anwaar .pptx
islamicknowledge5224
 
Networking_Essentials_version_3.0_-_Module_7.pptx
Networking_Essentials_version_3.0_-_Module_7.pptxNetworking_Essentials_version_3.0_-_Module_7.pptx
Networking_Essentials_version_3.0_-_Module_7.pptx
elestirmen
 
ARTIFICIAL INTELLIGENCE.pptx2565567765676
ARTIFICIAL INTELLIGENCE.pptx2565567765676ARTIFICIAL INTELLIGENCE.pptx2565567765676
ARTIFICIAL INTELLIGENCE.pptx2565567765676
areebaimtiazpmas
 
Internet_of_Things_Presentation_by-Humera.pptx
Internet_of_Things_Presentation_by-Humera.pptxInternet_of_Things_Presentation_by-Humera.pptx
Internet_of_Things_Presentation_by-Humera.pptx
cshumerabashir
 
How to Make Money as a Cam Model – Tips, Tools & Real Talk
How to Make Money as a Cam Model – Tips, Tools & Real TalkHow to Make Money as a Cam Model – Tips, Tools & Real Talk
How to Make Money as a Cam Model – Tips, Tools & Real Talk
Cam Sites Expert
 
Google_Cloud_Computing_Fundamentals.pptx
Google_Cloud_Computing_Fundamentals.pptxGoogle_Cloud_Computing_Fundamentals.pptx
Google_Cloud_Computing_Fundamentals.pptx
ektadangwal2005
 
Ad

rest-api-basics.pptx

  • 2. TRADITIONAL WEB APPLICATIONS Client Server GET /the-resource ... 200 OK Code... Displays the page, then user clicks on link. GET /another-resource ... 200 OK Code... 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:

    Claire

    Claire is 24 years old and lives in Boston.

    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 OF WEB 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 State Transfer, REST. • Clients can apply CRUD 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 State Transfer" is intended to evoke an image of how a well-designed Web 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 Claire 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 3 Cecilia 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 Amanda 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