Last updated April 7, 2014. Created by linclark on September 23, 2013.
Edited by TangMonk, kim.pepper, klausi, anavarre. Log in to edit this page.

Follow these steps to expose resources to GET requests:

  1. Add the configuration
  2. Set permissions
  3. Test with a GET request

Configuration

First make sure that REST module is enabled.

You may skip the steps below as by default all operations on entity:node are enabled. Below are the steps needed if you want to fine-tune your settings.

  1. Copy all of the files in your active config directory to your staging directory
  2. Make the needed changes to your rest.settings.yml file as shown below
  3. Go to admin/config/development/configuration to sync the configuration
  4. Clear caches

# Example configuration for enabling REST resources.
resources:
  # Enable the node resource.
  'entity:node':
    GET:
      supported_formats:
        - json
      supported_auth:
        - basic_auth
  # Enable the taxonomy term resource.
  'entity:taxonomy_term':
    GET:
      supported_formats:
        - json
      supported_auth:
        - basic_auth

Setting permissions

Go to /admin/people/permissions#module-rest and grant the corresponding REST permission to a user role you would like to be able to access the resource. Note: entity and field access still applies on top of that.

This can also be done with drush:

drush role-add-perm anonymous 'restful get entity:node'

Test with a GET request

You can use many clients to test requests.

cURL

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

Guzzle

<?php
use Guzzle\Http\Client;
$client = new Client('http://drupal-8.localhost');
// If in a Drupal environment use the HTTP client service.
$client = Drupal::httpClient()->setBaseUrl('http://drupal-8.localhost');
$request = $client->get('node/1');
$request->addHeader('Accept', 'application/hal+json');
$response = $request->send()->json();
print_r($response);
?>

Dev HTTP Client

An easy way to test is using a browser extension such as Dev HTTP Client. This exposes options for all of the HTTP headers that you may need to use.

GET request with Dev HTTP client

AttachmentSize
dev-http-client.png329.03 KB

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

I find the file of rest.setting.yml already at action folder

sites/default/files/config_Id5EEriLaELI_GeqOGo2hNITUJHMV6C3EdEGEC90kEIqUe4cD0vdGsratB6OR9HnoEwwgblCtg/active/rest.settings.yml :

resources:
  'entity:node':
    GET:
      supported_formats:
        - hal_json
      supported_auth:
        - basic_auth
    POST:
      supported_formats:
        - hal_json
      supported_auth:
        - basic_auth
    PATCH:
      supported_formats:
        - hal_json
      supported_auth:
        - basic_auth
    DELETE:
      supported_formats:
        - hal_json
      supported_auth:
        - basic_auth

I try to execute curl --request GET http://localhost/drupal8/node/1 -H 'Accept: application/hal+json' , but alert me: A fatal error occurred: Unable to find the controller for path "/node/1". Maybe you forgot to add the matching route in your routing configuration?