Definitions


REpresentational
State
Transfer

Server responds to requests with a representation of a resource (typically as JSON).


# Origin

  • Roy Fielding identified 5 architectural concepts that make the web successful in his dissertation (2000):
    • Addressable Resources
    • A Uniform, Constrained Interface
    • Representation-Oriented
    • Communicate Statelessly
    • Hypermedia As the Engine Of Application State (HATEOAS)

# Addressable Resources

  • Before REST was introduced there was often just one address where clients sent requests to.
  • The functionality or resource was specified within the message body.
  • REST uses the URI concept for distinguishing between resources within one service
    • people-service.com/students
    • people-service.com/teachers

# Uniform, Constrained Interface

  • Use a limited number of operations to manipulate resources.
HTTP-MethodDescription
GETRead-only, Idempotent, Secure, State will be unchanged
POSTAdd a new resource. Not idempotent: Every call will change state
PUTCreate a resource if it doesn’t exist, update it if it exists. Idempotent!
DELETEDeletes a resource. Idempotent
HEADJust as GET, but only status codes will be returned. Idempotent
OPTIONSList which methods are supported for a resource. Idempotent

# Excursus: Idempotence

An operation is idempotent if it can be applied multiple times without changing the result beyond the initial execution. When the state changes - it is not idempotent

Idempotence on Wikipedia


# Uniform, Constrained Interface - Advantages

  • Simplicity
    • Easy to understand
    • Lowers need for detailed interface description
    • No complex client libraries are needed
  • Interoperability
    • HTTP-Client-Library is sufficient for accessing REST services and available for practically every programming language or platform
  • Scalability
    • Results of read-only operations (GET, HEAD, OPTIONS) can be cached by clients and proxy-servers

# Representation-Oriented

  • Client and server exchange representations of resources using HTTP requests and responses.
  • Representation is not standardized
  • Commen formats:
    • JSON
    • XML
    • YAML
  • HTTP-Header ‘Content-Type’ defines which format will be used:
    • Content-Type: application/json
  • Using HTTP-Header ‘Accept’, the client can specify the preferred format (e.g. if the server supports multiple formats)
    • Accept: application/json

# Communicate statelessly

  • A single request is independent from previous and following requests
  • Server keeps no state for clients

# HATEOAS

  • Hypermedia As the Engine Of Application State
  • Servers provide information about resources dynamically
  • Subsequent requests can be made using the information from the response
GET /accounts/12345 HTTP/1.1
Host: bank.example.com
{
    "account": {
        "account_number": 12345,
        "balance": {
            "currency": "usd",
            "value": 100.00
        },
        "links": {
	        // links - for more informations
            "deposits": "/accounts/12345/deposits",
            "withdrawals": "/accounts/12345/withdrawals",
            "transfers": "/accounts/12345/transfers",
            "close-requests": "/accounts/12345/close-requests"
        }
    }
}

# Designing RESTful Web-Services

# Define object model

# Model URIs

  • /employees: all employees
  • /employees/{id}: specific employee
  • /logbookentries: all entries
  • /logbookentries/{id}: specific entry
  • /employees/{id}/logbookentries: all logbookentries for specific employee

# Semantics of HTTP-Methods

  • GET /employees[?startIndex=0&size=5]
    • Read (a part of) all employee objects
    • [?key = value] query parameters
  • GET /employees/{id}
    • Read an employee object via its key
  • POST /employees
    • Insert an employee object. Key will be generated and (usually) returned.
  • PUT /employees/{id}
    • Update an employee object. Create if not exists (recommendation).
  • DELETE /employees/{id}
    • Delete an employee object (or mark as deleted)

# HTTP and REST

  • Following the principles by Roy Fielding a RESTful service can use basically any communication protocol.
  • However in practice, REST is basically only used with HTTP

# Structure of a HTTP request

GET /employees HTTP/1.1
Host: employee-service.com
Accept: application/json
User-Agent: Mozilla/5.0
PUT /employees/42 HTTP/1.1
Host: employee-service.com
Content-Type: application/json
Accept: application/json
 
{
  "firstName": "Fritz",
  "lastName": "Phantom"
}

# HTTP Response

A response consists of

  • a status code
  • response headers
  • a body

# Status code categories

CategoryDescription
1xxInformational response. Processing continues
2xxSuccess
3xxRedirection
4xxClient errors. (Error seems to have been caused by client)
5xxServer errors.

Status CodeMessageDescription
101Switching Protocolse.g. when opening WebSocket connection
200OKRequest was handled successfully
201CreatedRequest was successful, a resource has been created.
301Moved PermanentlyRedirects permanently to location specified in Location Header
400Bad RequestRequest is malformed
401UnauthorizedUser not logged in or no authorization information provided.
403ForbiddenUser is logged in but not permitted to access the resource.
404Not FoundRequested resource could not be found
418I’m a teapotApril fools joke by the IETF
500Internal Server ErrorUnspecified error on the server happened.