KEMBAR78
REST Api with Asp Core | PDF
REST API in ASP Core
Irina Scurtu
.NET Technical Lead
Trainer @ Scoala Informala
@Fortech
AGENDA
What is REST| 1
REST Constraints| 2
HTTP Status Codes| 3
HTTP Headers | 4
Richardson Maturity Model | 5
Copyright 2017 © Fortech. All rights reserved.
WHAT IS REST
REST
▪ Architectural style
▪ The architectural style of web
▪ Everything is a resource
Did you know?
Did you know?
REST
CONSTRAINTS
CLIENT-SERVER
▪ We need to have a server
▪ We need to have a client
▪ The server is not aware of the client
▪ The server does data transformation
under the hood
▪ The client simply uses the resources
Client
Server
Request/Response
STATELESS
State
Cookie
Session
Hidden
fields
Others?
STATELESS
▪The server keeps no state
▪ No identifier, no trace at all
▪If there is a trace, it’s contained in the request
▪ Eg: authentication details
▪The server treats each request as a new one
CACHE
▪The most important spec of the HTTP
▪The server must mark the response as cacheable
or not cacheable
▪It may increase the perceived performance
▪It may save database roundrips especially if the
data doesn’t change often
▪Done through headers
LAYERED SYSTEM
▪ The client can talk with an intermediate
▪ It only receives the resource
▪ It doesn’t matter if there is a load balancer involved or
a proxy, or a gateway
UNIFORM RESOURCE
▪Everything is a resource
▪A resource is anything that can be named
▪Every resource must have an URL
▪ Any document that can be read on the client, is a
representation of a resource
▪ Has 4 subconstraints
1.IDENTIFICATION OF RESOURCES
▪A resource needs to be addressable
▪A resource doesn’t correspond to a database table
▪It can be a subset, or a transformed entity
1.IDENTIFICATION OF RESOURCES
1.IDENTIFICATION OF RESOURCES
▪ /getLocation
▪ /newCat
▪ /feedCat
▪ /getAllCats
▪ /getBlackCats
▪ /getHungerLevel
▪ /getCatOwners
BAD!!!!
Hard to
remember!
2.MANIPULATION OF RESOURCES THROUGH
REPRESENTATIONS
▪A client doesn’t interact with the domain objects
▪It doesn’t query the database directly
▪It sends a representation of a resource to be
updated
3.SELF-DESCRIPTIVE MESSAGES
▪Each request/response includes details and
meaning
▪Each request/response must be able to be
understood in isolation
▪Add meaning through:
3.SELF-DESCRIPTIVE MESSAGES
Request/Response
Verbs
Media
types
Headers
Status
Codes
4.HATEOAS
▪Hypermedia as engine of application state
▪ We should use the links( hypermedia with text) to
navigate the app
▪ A response should include a links section that allows
the hierarchical navigation
{
"links": {
{“rel": “self” , "href": "/superheroes" },
{“rel”: "next“, "href": "/superheroes?page=2" }
}
}
4.HATEOAS
Superheroes?page=2 superheroes
superheroes/3
superheroes/3/friends
Superheroes/3/friends/2
4.HATEOAS
root
/superheroes
By Id
/superHeroes/3
Relational
/superheroes/3/friends
Let’s talk about
endpoints
2 URLs for each resource
A collection
/superheroes/getAll
vs
/superheroes
An element
/superheroes/getSuperhero?id=1
vs
/superheroes/1
Guidelines
▪ Cool URIs don't change
▪ Use nouns not verbs
▪ Plural not singular
▪ Concrete
/things vs /dogs
▪ Variations - > query strings - not different endpoints
Status Codes
OK ?! 200
Or When OK really means OK
201
202 - Accepted
- For further processing
- For enqueuing something
204 – No content
- After deleting a resource
401
403
- Identified but not allowed to perform an
operation
Inapropriate picture
404
405 – Not Allowed
406 – Not Acceptable
- Accept header – xml
- Server does only json
Server Client
Sends only xml
Knows to read
only json
409
415
Accept header has a media type you
don’t support
Header fields
A suuuuuper small list
Accept
▪ The mime type the client prefers and understands
Accept : text/xml
Accept : application/json
Content-Type
▪ The mime type of the request body
▪ Describes the content of the request
Content-Type : text/xml
Location
▪ After a non-idempotent operation
Cache-Control
Cache-Control : no-cache
max-age=x(seconds)
Richardson
Maturity Model
Level 0
▪ YOU KNOW how to use it
▪ Single endpoint
• Go to a restaurant and order pizza, curry and Chinese
The food is good?
Level 1
▪ One verb - different locations
• Go to the italian restaurant to order pizza
• Go to the Chinese restaurant to order Chinese food
• Go to the Indian restaurant to order curry
Level 2
▪Uses Http Verbs and multiple resource endpoints
• Order a pizza from Italian restaurant
• After 5 minutes change the order to add extra
mozzarella
Level 3
▪Http Verbs and multiple resource endpoints
+ options
• Order a pizza
• Get the pizza
• Waiter gives info about the available toppings for
the pizza
• Informs you that they have other pizza types
RMM
HTTP verbs+ URI +
Hypermedia = HATEOAS
HTTP verbs + multiple URI
single verb +multiple URI
One Verb + one URI
WHY - Benefits
▪ ?
▪ ?
▪ ?
▪ ?
THANK YOU
Q&A
REST Api with Asp Core

REST Api with Asp Core