SlideShare a Scribd company logo
REST beyond CRUD
Paulo Sousa
@pagsousa
 Sample REST API for “more than CRUD”
 Examples
 Shopping Cart
 Book printing
Shopping cart
 Create a new cart
 Obtain its contents
 Add an item
 Remove an item
 Empty the cart
 Calculate full charges
 Proceed to checkout
Base URI
 Cart as top level resource
 http://example.org/carts/
Or
 Cart as sub resource of user
 http://example.org/users/{id}/cart
Creating a cart
 Cart as top level resource
POST example.org/carts/
 Would return a new cart resource URI
201 Created
Location: http://example.org/carts/123
 Cart as sub resource of user
 Not necessary, cart is already available
 http://example.org/users/{id}/cart
Obtain the content of the cart
 Issue a GET to the resource
GET /carts/{id}
or
GET /users/{id}/cart
 On begining, would return a representation of the empty cart
200 Ok
{itens: []}
 Otherwise would return the current content of the cart
200 Ok
{itens: [{id:..., qty:...}, ...]}
Add an item
 PUT an updated representation of the entire cart
PUT /carts/{id}
{itens: [{id: ..., qty: ...},{id: ..., qty: ...}]}
Or
 Treat the cart as a collection and POST a new item
POST /carts/{id}/itens
{id: ..., qty: ...}
 Would return the URI of the newly added item
201 Created
Location: http://example.org/carts/123/itens/456
Remove an item
 PUT an updated representation of the entire cart
PUT /carts/{id}
{itens: [{id: ..., qty: ...}]}
Or
 Treat the cart as a collection and DELETE an item
DELETE /carts/{id}/itens/{itemid}
Empty the cart
 PUT an updated representation of the empty cart
PUT /carts/{id}
{itens: []}
Or
 Treat the cart as a collection and DELETE the itens
DELETE /carts/{id}/itens
Calculate full charges
 If the calculation is transitory, just GET the result of the calculation
GET /carts/{id}/fullcharges
or
GET /users/{id}/cart/fullcharges
 Would return
{itens: 100, taxes: 23, shipping: 12, total: 140, currency:
EUR}
 Or an updated shopping cart representation with charges element
{itens: [...], charges: {...}}
 This element should be considered transient and not part of the resource
 Former approach is “better” as it is a separate resource
Calculate full charges (2)
 If charges are to be considered a persistent part part of the
resource
 By doing the calculation you are in fact changing the resource
 i.e., Have side effect, so GET is to be avoided
 POST the request for the calculation,
 e.g.
POST /carts/{cid}
{fullcharges: yes}
 Would return updated shopping cart
{itens: [...], charges: {...}}
Procceed to checkout
 POST an order with the content of the shopping cart?
GET /carts/{id}/as-order
POST /orders
Or
 POST to a check-out controller?
POST /carts/{id}/checkout
or
POST /checkout?cart={id}
or
POST /salesprocess/checkout
Book printing example
 Get a pdf of a book
 Print a book
Book printing example
 Download a pdf representation of a book for
printing at the client
GET /books/{id}?template={tmpl}
Accept: application/pdf
Book printing example
 Print a book (at the server)
POST /books/{id}/print?printer=hp01
 In fact what we want is to order a printing service...
POST /printorders/
{what: “/books/{id}”, quality: ..., ... }
202 Accepted
Location: /printorders/3Af42X
Not very
beatiful
Book printing example
 Query the status
GET /printorders/{id}
200 Ok
{status = “pending”, ...}
Book printing example
 Cancel the printing order
DELETE /printorders/{id}
200 Ok
 If the order could not be deleted anymore
(e.g., already printing), the service would
return a 405 Method Not Allowed

More Related Content

PPTX
RESTful services Design Lab
PDF
Rest web services
PDF
PoEAA by Example
PDF
Lição prova professor coordenador
PPTX
Patterns for distributed systems
PDF
Enterprise Integration Patterns
PDF
Software Product Lines
PDF
OO design principles and patterns
RESTful services Design Lab
Rest web services
PoEAA by Example
Lição prova professor coordenador
Patterns for distributed systems
Enterprise Integration Patterns
Software Product Lines
OO design principles and patterns

Viewers also liked (7)

PPTX
Benefits of Hypermedia API
PDF
Decoupled Communication
PDF
Communication
PPTX
Principles of Service Orientation
PPTX
Design Patterns: From STUPID to SOLID code
PPT
Modern web architectural patterns
PDF
Patterns of Enterprise Application Architecture (by example)
Benefits of Hypermedia API
Decoupled Communication
Communication
Principles of Service Orientation
Design Patterns: From STUPID to SOLID code
Modern web architectural patterns
Patterns of Enterprise Application Architecture (by example)
Ad

More from Paulo Gandra de Sousa (9)

PDF
Introduction to Microservices
PPTX
Minds-on DDD
PPTX
Introduction to microservices
PPTX
Design Patterns: Back to Basics
PPTX
Hypermedia APIs
PPTX
Revision control with Mercurial
PDF
Documenting Software Architectures
PDF
models of distributed computing
PDF
Distributed Systems
Introduction to Microservices
Minds-on DDD
Introduction to microservices
Design Patterns: Back to Basics
Hypermedia APIs
Revision control with Mercurial
Documenting Software Architectures
models of distributed computing
Distributed Systems
Ad

Recently uploaded (20)

PDF
cuic standard and advanced reporting.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
A Presentation on Artificial Intelligence
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
KodekX | Application Modernization Development
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Approach and Philosophy of On baking technology
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
cuic standard and advanced reporting.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
NewMind AI Weekly Chronicles - August'25 Week I
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
A Presentation on Artificial Intelligence
Reach Out and Touch Someone: Haptics and Empathic Computing
KodekX | Application Modernization Development
The Rise and Fall of 3GPP – Time for a Sabbatical?
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
20250228 LYD VKU AI Blended-Learning.pptx
Network Security Unit 5.pdf for BCA BBA.
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Chapter 3 Spatial Domain Image Processing.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Approach and Philosophy of On baking technology
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Unlocking AI with Model Context Protocol (MCP)

REST beyond CRUD

  • 1. REST beyond CRUD Paulo Sousa @pagsousa
  • 2.  Sample REST API for “more than CRUD”  Examples  Shopping Cart  Book printing
  • 3. Shopping cart  Create a new cart  Obtain its contents  Add an item  Remove an item  Empty the cart  Calculate full charges  Proceed to checkout
  • 4. Base URI  Cart as top level resource  http://example.org/carts/ Or  Cart as sub resource of user  http://example.org/users/{id}/cart
  • 5. Creating a cart  Cart as top level resource POST example.org/carts/  Would return a new cart resource URI 201 Created Location: http://example.org/carts/123  Cart as sub resource of user  Not necessary, cart is already available  http://example.org/users/{id}/cart
  • 6. Obtain the content of the cart  Issue a GET to the resource GET /carts/{id} or GET /users/{id}/cart  On begining, would return a representation of the empty cart 200 Ok {itens: []}  Otherwise would return the current content of the cart 200 Ok {itens: [{id:..., qty:...}, ...]}
  • 7. Add an item  PUT an updated representation of the entire cart PUT /carts/{id} {itens: [{id: ..., qty: ...},{id: ..., qty: ...}]} Or  Treat the cart as a collection and POST a new item POST /carts/{id}/itens {id: ..., qty: ...}  Would return the URI of the newly added item 201 Created Location: http://example.org/carts/123/itens/456
  • 8. Remove an item  PUT an updated representation of the entire cart PUT /carts/{id} {itens: [{id: ..., qty: ...}]} Or  Treat the cart as a collection and DELETE an item DELETE /carts/{id}/itens/{itemid}
  • 9. Empty the cart  PUT an updated representation of the empty cart PUT /carts/{id} {itens: []} Or  Treat the cart as a collection and DELETE the itens DELETE /carts/{id}/itens
  • 10. Calculate full charges  If the calculation is transitory, just GET the result of the calculation GET /carts/{id}/fullcharges or GET /users/{id}/cart/fullcharges  Would return {itens: 100, taxes: 23, shipping: 12, total: 140, currency: EUR}  Or an updated shopping cart representation with charges element {itens: [...], charges: {...}}  This element should be considered transient and not part of the resource  Former approach is “better” as it is a separate resource
  • 11. Calculate full charges (2)  If charges are to be considered a persistent part part of the resource  By doing the calculation you are in fact changing the resource  i.e., Have side effect, so GET is to be avoided  POST the request for the calculation,  e.g. POST /carts/{cid} {fullcharges: yes}  Would return updated shopping cart {itens: [...], charges: {...}}
  • 12. Procceed to checkout  POST an order with the content of the shopping cart? GET /carts/{id}/as-order POST /orders Or  POST to a check-out controller? POST /carts/{id}/checkout or POST /checkout?cart={id} or POST /salesprocess/checkout
  • 13. Book printing example  Get a pdf of a book  Print a book
  • 14. Book printing example  Download a pdf representation of a book for printing at the client GET /books/{id}?template={tmpl} Accept: application/pdf
  • 15. Book printing example  Print a book (at the server) POST /books/{id}/print?printer=hp01  In fact what we want is to order a printing service... POST /printorders/ {what: “/books/{id}”, quality: ..., ... } 202 Accepted Location: /printorders/3Af42X Not very beatiful
  • 16. Book printing example  Query the status GET /printorders/{id} 200 Ok {status = “pending”, ...}
  • 17. Book printing example  Cancel the printing order DELETE /printorders/{id} 200 Ok  If the order could not be deleted anymore (e.g., already printing), the service would return a 405 Method Not Allowed