JavaScript and Drupal 8 RESTful Web Services

This page aims to outline how to use JavaScript to communicate with Drupal 8's RESTful services. It is broken into sections per entity type. It outlines which HTTP method to use (GET, POST, PATCH, or DELETE), the Drupal path to use, which Content-type and Accept headers (see change record #2501221) should be set, and what response can be expected from the server.

HAL: Serialization using the Hypertext Application Language

The HAL module is included in Drupal core. When enabled, the HAL module serializes entities using HAL (Hypertext Application Language).

What is it?

For general information about the language, see the informal or formal (06) HAL specification.

Videos demonstrating the usage of services 3.x

Links to video tutorials are here and here. This one demonstrates the usage of setting up a service for a image field gallery setup from services views and then using a titanium application to http GET the images.

Feeds JSONPath Parser

1) Identify the json URL, and look at it using this tool: http://jsonviewer.stack.hu/ (or, if you know the json well, identify what you want to import.

2) Download jsonpath php file here: http://jsonpath.googlecode.com/files/jsonpath-0.8.1.php and place inside sites/all/modules/feeds_jsonpath_parser

3) Create a new parser using feeds, with the jsonpath parser selected

4) Example json:

{
  "tumblelog": {
    "title": "Tumblr Blog",
    ..... (other global fields)
  },
  "posts": [
    {
      "id": "46252144726",
      "url": "http://whatever.com",
      "type": "regular",
      "date": "Mon, 25 Mar 2013 10:45:38",
      "regular-title": "Test Blog Entry",
      "regular-body": "Body text goes here",
      "tags": [
        "tag1",
        "tag2",
      ]
    },
   {
      "id": 46249331035,
.... rest of posts

OK, so now is where I'm getting unclear, and need help.

5) In the jsonpath parser settings page:admin/structure/feeds/rss_feed_importer/settings/FeedsJSONPathParser there are two elements, "Context" and "Title". In the Context field, if you want to map the variables in post, to fields in a blog content type, for example, you need to let jsonpath parser know where to start. So in this case, the context would be:

$.tumblelog.posts.*

Serialization API in Drupal 8

Drupal 8 can output JSON and XML versions of data using the Serialization API. Modules can add new serialization formats, as core's HAL (#1924854: [META] Hypertext Application Language (HAL) support) module will.

Serializing data

To serialize data, get the serializer from the container.

RESTful Web Services

RESTWS exposes Drupal resources (e.g. entities) as RESTful web services. The module makes use of the Entity API and the information about entity properties (provided via hook_entity_property_info()) to provide resource representations.

It aims to be fully compliant to the REST principles.

Installing the Module

  • Copy the whole restws directory to your modules directory (e.g. DRUPAL_ROOT/sites/all/modules) and activate the RESTful Web Services module.
  • Use Drush commands: drush dl restws & drush en restws
  • There is no user interface to configure.

Usage / Testing

To obtain the JSON representation of an entity use your browser to visit: http://example.com/<entity type name>/<entity id>.json
For example: http://example.com/node/1.json or http://example.com/user/1.json would resolve as a JSON dump to client. There are no "service endpoints" to configure as resources are just available at uniform paths like "node/1", "user/1". One can use a client in the form of a browser plugin to test out the different CRUD functions.

Permissions

Pages

Subscribe with RSS Subscribe to RSS - json