REST and Serialization in Drupal 8 (DrupalCon Portland Session Notes)

Wayne Eaker
Module Developers
REST and Serialization in Drupal 8 (DrupalCon Portland Session Notes)

What is REST in Drupal 8?

  • Accept header: "Accept: application/json" at /node/###

Four components of REST

  • Base formats: JSON, XML
  • URIs: URLs for a resource. Link Relation: link to other entities
  • Media Type: application/json, application/hal+json (_links and _embedded), application/vnd.github+json
  • HTTP Method: GET, POST, PATCH, DELETE. PATCH instead of PUT...update some fields, but not the whole entity

What is in Drupal 7 core right now?

  • Services module
  • Endpoint concept and custom URL paths
  • Caters to RPC and SOAP
  • drupal_form_submit() for write operations
  • Only covers nodes, comments, users
  • No hypermedia controls
  • RESTWS Module
  • Exposes all entities
  • No endpoints

What is in Drupal 8 core right now?

  • RESTful Web Servcies module
  • Serialization module
  • HAL module
  • POST, GET, PATCH, DELETE
  • Access control on the field and entity level
  • Authentication: not handled by REST module
  • User object, permissions applied with standard session cookie.
  • Views support - Display plugin for REST

Formats in Core

  • JSON
  • XML (kinda)
  • Hypertext Application Lanaguage (HAL)

Symfony Serializer

  • $serializer->serialize($object, $format, $context);
  • You can write normalizer to change things like "field_image: 5" to "field_image: 'foo/bar.png'"

Hypermedia Controls

  • Allows server to change URL schemes without breaking clients
  • Discoverability: by returning the possible links, client developers can find out about new URIs.