Restful in Drupal 8
Nguyen Tien Si
GO1
Why is RESTful important?
The old Web

Image source: blog.xrtml.org
Request
GET /node/1 HTTP/1.1
Host: d8.local:8083
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36
Referer: http://d8.local:8083/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,vi;q=0.6Response
HTTP/1.1 200 OK
Date: Mon, 08 Sep 2014 09:02:26 GMT
Server: Apache/2.4.9 (Unix) mod_fcgid/2.3.9
X-Powered-By: PHP/5.5.15
Cache-Control: must-revalidate, no-cache, post-check=0, pre-check=0, private
X-Drupal-Cache-Tags: theme:bartik theme_global_settings:1 block_view:1 block:bartik_powered block:bartik_footer block:bartik_login block:bartik_content block_plugin:system_powered_by_block block_plugin:system_menu_block__footer block_plugin:user_login_block block_plugin:system_main_block rendered:1 menu:footer menu:main node_view:1 node:1 user:1 taxonomy_term:2 taxonomy_term:1 filter_format:basic_html user_view:1
X-Generator: Drupal 8 (http://drupal.org)
X-UA-Compatible: IE=edge,chrome=1
Content-language: en
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8Response

No longer rendering HTML

Mobile First

Image source: http://brickwicks.co.uk/
What is REST?
Web Services
"A Web service is a method of communication between two electronic devices over a network (the WWW)."
from Wikipedia.
REST
"Representational state transfer (REST) is an abstraction of the architecture of the World Wide Web; more precisely, REST is an architectural style consisting of a coordinated set of architectural constraints applied to components, connectors, and data elements, within a distributed hypermedia system."
from Wikipedia.

From https://restful-api-design.readthedocs.org
Model

GET method
REST Components
- Base Format - JSON
- HTTP Method - GET
- URI - node/1
- Media type - application/hal+json

COMPONENTS
HTTP Method
- GET - Fetch this page
- POST - Have some data, need insert
- PATCH - Have some changed field, need update
- DELETE - Want delete this item
PATCH vs PUT
The semantics of this call are like PUT inthat it updates a resource, but unlike PUT, it applies a delta rather than replacing the entire resource.
Recommendation:
- Provide PATCH and PUT
- Make PATCH do an relative update and have PUT replace the entire resource.

POST

DELETE

GET
HAL Specification
HAL is a simple format that gives a consistent and easy way to hyperlink between resources in your API.
REST in Drupal
Drupal 7 - in contrib
- Services - https://www.drupal.org/project/services
- Restws - https://www.drupal.org/project/restws
Drupal 8 - in core
- RESTful Web Services (rest)
- Serialization (serialization)
- Hypertext Application Language (hal)
- HTTP Basic Authentication (basic_auth)

Web Services module list
Resource & Operations
Create - POST - /entity/node
Read - GET - /node/1
Update - PATCH - /node/1
Delete - DELETE - /node/1
Authentication - in core
- Cookie
- Returns authenticated or anonymous user depending on the presence of a cookie
- Basic Auth
- Checks if user & password are in the request headers and finds a matching user in the DB.
Authentication - in contrib
Auth Providers in contrib: OAuth
See more: https://www.drupal.org/node/2110825
Restful Views in Drupal 8


References
- https://www.drupal.org/documentation/modules/rest
- http://www.acquia.com/node/3168256
- http://buytaert.net/the-future-is-a-restful-drupal
- https://drupalize.me/blog/201401/introduction-restful-web-services-drupal-8
- http://lin-clark.com
- http://en.wikipedia.org/wiki/Web_service
- http://en.wikipedia.org/wiki/Representational_state_transfer
- http://stateless.co/hal_specification.html
- http://restful-api-design.readthedocs.org/en/latest/methods.html
- http://en.wikipedia.org/wiki/Internet_media_type
Question?
Restful in Drupal 8
By Nguyen Tien Si
Restful in Drupal 8
Restful in Drupal 8
- 1,552