Drupal 8 – Setup the RESTful Service @ 1

After studying Drupal 8 for a few days, i got the feeling that there are still a lot of works to polish it before a beta or RC release. And lacking documentation is another big problem to kick start with Drupal 8. This post may help you to get some simple idea on how to setup RESTful service in Drupal 8 but very likely, the details will change in the future.

1. Enable the RESTful Web Services.
drupal-8-setup-rest-service-1
 

2. Copy the all the files in sites/files/config_HASH/active to sites/files/config_HASH/staging.

3. Edit the rest.settings.yml in the staging directory as follow.

# Example configuration for enabling REST resources.
resources:
  # Enable the node resource.
  'entity:node':
    # Enable reading nodes in all formats known.
    GET: { }
  'entity:taxonomy_term':
    # Enable reading terms in all formats known.
    GET: { }

 

4. Go to admin/config/development/sync to import the new config file change.
drupal-8-setup-rest-service-2
 

5. Go to admin/people/permissions#module-rest and enable the Access GET on Content resource for Anonymous User.
drupal-8-setup-rest-service-3
 

6. Open the terminal and try to get the node content in JSON by the curl command.

curl -H "Accept: application/json" --request GET http://drupal8.localhost.com/entity/node/1

drupal-8-setup-rest-service-4
 

7. This is the returned JSON.

{
  "nid":[
    {
      "value":"1"
    }
  ],
  "uuid":[
    {
      "value":"d145fc48-5f95-459f-aeae-8dc834fbe065"
    }
  ],
  "vid":[
    {
      "value":"1"
    }
  ],
  "type":[
    {
      "value":"article"
    }
  ],
  "langcode":[
    {
      "value":"en"
    }
  ],
  "title":[
    {
      "value":"Article 1"
    }
  ],
  "uid":[
    {
      "target_id":"1"
    }
  ],
  "status":[
    {
      "value":"1"
    }
  ],
  "created":[
    {
      "value":"1376670139"
    }
  ],
  "changed":[
    {
      "value":"1376670165"
    }
  ],
  "comment":[
    {
      "value":"2"
    }
  ],
  "promote":[
    {
      "value":"1"
    }
  ],
  "sticky":[
    {
      "value":"0"
    }
  ],
  "tnid":[
    {
      "value":"0"
    }
  ],
  "translate":[
    {
      "value":"0"
    }
  ],
  "revision_timestamp":[
    {
      "value":"1376670165"
    }
  ],
  "revision_uid":[
    {
      "target_id":"1"
    }
  ],
  "log":[
    {
      "value":""
    }
  ],
  "body":[
    {
      "value":"<p>Hello World~&nbsp;<\/p>\r\n",
      "format":"basic_html",
      "summary":""
    }
  ],
  "field_image":[
    {
      "fid":null,
      "alt":null,
      "title":null,
      "width":null,
      "height":null
    }
  ],
  "field_tags":[
    {
      "target_id":null
    }
  ]
}

 

8. If you want to use the Hypertext Application Language (HAL), you could enable the HAL module and use application/hal+json in the curl command.

curl -H "Accept: application/hal+json" --request GET http://drupal8.localhost.com/entity/node/1

 

Done =)

Reference:

Advertisements

2 thoughts on “Drupal 8 – Setup the RESTful Service @ 1”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s