Integrate REST Webservice Using Guzzle In Drupal 8

Integrate-REST-webservice-using-Guzzle-in-Drupal-8
Default Avatar
By Jayesh Makwana
Drupal 8 Core includes Restful Web Service module. It builds from Drupal 7 Restful Web Service below are the basic points of the REST in Drupal 8. For the purpose of this article, all the screenshots are for 8.3.x.

Table of Contents

About Author

Default Avatar

Jayesh Makwana

Experienced Drupal developer and content creator. Passionate about open source technologies and web development best practices.

1. Access http://localhost/admin/config/services/rest for listing all links of REST UI. rest ui configure drupal 8 Click on configure link, you will see the following screen. Please click on Edit link for configuration of this link. drupal 8 rest resources   After that, you will see following configuration for that specific REST. settings for resource content drupal
<pre>
$client =$this->http_client;
$response = $client ->get('http://localhost/node/1?_format=json');
$json_string =  json_decode($response->getBody());
</pre>
/* Get Session Token Start Code */ $client =$this->http_client; $response = $client ->get('http://localhost:8888/drupal-8.3.4/rest/session/token'); $token_string = (string) ($response->getBody()) ; /* Get Session Token End Code*/ $serialized_entity = json_encode([ 'title' => [['value' => 'Example Article']], 'type' => [['target_id' => 'article']], '_links' => ['type' => [ 'href' => 'http://localhost/rest/type/node/article' ]], ]); $response = $client ->post('http://localhost/entity/node?_format=hal_json', [ 'auth' => ['user', 'password’], 'body' => $serialized_entity, 'headers' => [ 'Content-Type' => 'application/hal+json', 'X-CSRF-Token' => $token_string ], ]);
<pre>
 
/* Get Session Token Start Code */
  $client =$this->http_client;
  $response = $client
    ->get('http://localhost:8888/drupal-8.3.4/rest/session/token');
   $token_string =  (string) ($response->getBody()) ;
  /* Get Session Token End Code*/
 
 
$serialized_entity = json_encode([
  'title' => [['value' => 'Example Article UPDATED']],
  'type' => [['target_id' => 'article']], 
  '_links' => ['type' => [
      'href' => 'http://localhost/rest/type/node/article'
  ]],
]);
 
$response = $client
  ->patch('http://localhost/node/1?_format=hal_json', [
    'auth' => ['user', 'password’],
    'body' => $serialized_entity,
    'headers' => [
      'Content-Type' => 'application/hal+json',
      'X-CSRF-Token' => $token_string
    ],
  ]);
 
</pre>
<pre>
 
/* Get Session Token Start Code */
  $client =$this->http_client;
  $response = $client
    ->get('http://localhost/rest/session/token');
   $token_string =  (string) ($response->getBody()) ;
  /* Get Session Token End Code*/
 
$response = $client
  ->delete('http://localhost/node/1?_format=hal_json', [
    'auth' => ['user', 'password’],
     'headers' => [
      'X-CSRF-Token' => $token_string
    ],
  ]);
 
</pre>