Headless Drupal is one of the most exciting topics that captured many minds in the last few years. It’s a phenomenon closely related to RESTful API (also known as web services) — another popular term. It gives a wide range of new abilities for us as developers. We will look under the hood of the web services, how they work and how they can be used, go through the list of available tools. At the end of the article, we will use Drupal 8 Core REST API to create a node. So let’s try to deal with all those fancy words and determine how it can help us to make our sites better.
Assume you have two computers connected to the Internet, both are on the different continents. To communicate with each other they need a unified language. This is a key — the web service is a standardized way for two computers to interact. In the age of the ‘Internet of Things’ (IoT), anything connected to the Internet can be a computer (even your microwave oven).
So computers or devices can use the same API (an application program interface) to understand and interact with each other without any human intervention. We can use API’s in everyday life and don’t know about that (e.g. when we connect a phone to the computer).
For example, your mobile app can communicate with Drupal site to create a node or change and delete an existing one. That’s how it works: using REST API, the mobile app makes a request and Drupal site responds with structured data (e.g. JSON) which an external app can use. So, the external app can be a JavaScript framework, that used for front-end of site display, but all backend logics still belongs to Drupal. And even more…
REST is just another one way to make the Web Services work. There are also other formats such as XML-RPC, SOAP, etc.
Let’s take a look at REST abilities of Drupal 8.
In Drupal 8 Web Services work out of the box. That means you don’t need to download additional modules, all necessary tools are already implemented in the core with the following modules:
The shortest answer is doing the thing. — Ernest Hemingway.
First of all, you need to enable the modules listed above. With a little help from the REST module, you easily can do GET, POST, DELETE and PATCH operations on node entity resource.
Some words about HTTP request methods: when you open a page in your preferred web browser it uses the GET method to read a resource, retrieve data and give it back to you. If you want to create the resource on a server you need to use POST. The DELETE method deletes the specified resource. PATCH is used for the update.
So these are basic methods that will help us to interact with entities on our Drupal site. To use them you need to install a browser extension. I’m using Restlet Client — DHC for this purpose. My version of Drupal is 8.2.6.
Let’s try to create a node. In the request BODY, we set a type and a title of the created node. Content-Type header is set to application/hal+json. This is how POST to the URL /entity/node looks like:
Restlet Client:
cURL (command line):
curl — include \
— request POST \
— user admin:secret \
— header ‘Content-type: application/hal+json’ \
— header ‘X-CSRF-Token: <obtained from http://example.com/rest/session/token>' \
http://example.com/entity/node?_format=hal_json \
— data-binary ‘{“_links”:{“type”:{“href”:"http://drupal8.dev/rest/type/node/article"}},"title":[{"value":"My first article”}],”type”:[{“target_id”:”article”}]}’
Let me notice some things about CSRF token: you can get this token by the GET request to rest/session/token and send it with your POST request.
If done correctly you should see the nice green 201 Created response.
Now you should have an understanding of REST Web Services in Drupal 8 and how you can manipulate basic Drupal entities such as a node through REST API. And it is just the beginning. I hope this knowledge will help you in your own further exploration.
Originally posted at the ADCI Solutions website.
The author is Mikhail Zolenko, Web Developer at ADCI Solutions
Mikhail is always looking for a new challenge that will make his mind search for a simple and beautiful solution. He does all things with love and passion, whatever it is: coding, collecting, playing the guitar or listening to the good old music.
Follow us on social networks: Twitter | Facebook | LinkedIn
Migrate API: custom Drupal-to-Drupal migration_Sooner or later every developer faces this scary (actually, not) process called “Migration”. If you’re one of them, you…_hackernoon.com