Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
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.
What is a Web Service?
A little bit about the Internet of things, mobile apps and other stuff
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.
Web Services in 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:
- RESTful Web Services (rest): provides RESTful API that allows you to interact with any content entity such as nodes, comments, users. Depends on the Serialization module.
- Serialization: a service for serialization of data to and from formats, such as JSON or XML.
- Hypertext Application Language (hal): HAL is a hypermedia format which is a primary format in the Drupal 8 Core. Can be encoded in JSON or XML.
- HTTP Basic Authentication (basic_auth): provides a basic user authentication.
Example
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.
Conclusion
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
RESTful Web Services in Drupal 8 quick start guide was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.