Exposing Your API in Drupal 8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this lesson we're going to take a look at how Drupal 8 can help us quickly expose a REST API will do this by enabling and configuring the rest module and the rest UI module from contrib we'll also take a look at a high-level overview of how an API is created and served by core in Drupal 8 and by the end of the video you'll understand how the components in these four modules contribute to providing an out-of-the-box API without writing a single line of code will again be making heavy use of the postman Chrome extension so if you don't already have that installed you might want to do so let's get started the first thing we want to do in addition to downloading Drupal 8 core is to download the rest UI module the rest UI module will allow us to configure our API endpoints without having to dig in the ml files to do so here we're looking at a fresh installation of Drupal 8 along with the rest UI module let's login and get started now that we're logged in first things first we need to enable a few modules we'll go to the extend page we can find the rest modules down at the bottom of the page we're gonna want to enable how HTTP basic authentication rest UI restful web services and serialization while we're waiting for those modules to install let's take a look at how these moving pieces fit together this graphic is from a presentation done by Lynn Clark from Drupal con Prague in 2013 it describes the basic pipeline for rest responses in Drupal 8 if we trace an incoming request we can see that it's routed to the request Handler the request handler identifies that the rest module is responsible for this request and sends it to the deserialize err to normalize the data the request handler then matches up the request with the appropriate resource handler we'll take a look at an example of those from core in a little while this resource plug-in takes the data from the incoming request and retrieves the relevant information from the database or elsewhere when the resource plug-in returns data to the request handler it again interacts with the serializer before being sent along as a response the modules included in core that we just enabled will handle serialization for XML JSON and how Plus JSON it's probably not surprising that the serialization module is responsible for deserializing and serializing request and response data the hal module specifically ah hell plus json serialization plug in the HTTP basic authentication module provides an authentication plug-in which allows us to control access to our api resources based on HTTP method there will likely be other authentication mechanisms such as auth supported in the future via contribs the restful web services module handles the resource plug-in system as well as defining the initial resource routes a good example of a resource plug-in can be found in the DB log module which provides an API endpoint for watchdog entries rest UI which we took a look at is a super helpful contribs services configuration via the admin UI so we can configure our API endpoints without editing Hamill files now that we have all of our modules enabled let's take a look at that configuration screen we'll go to configuration scrolling down under services we'll see a rest item here's the list of resources provided to us by the restful Web Services module for now let's just look at the content resource when I click Edit I'm taken to a configuration page where I can select which HTTP verbs this API should respond to as well as which formats in which authentication methods we should work with for each verb once we're happy with how we've configured our API let's jump over to the permissions page to get permission set up as well scrolling down we can see that several new permissions have been added by the rest web services module for each type of resource we've enabled we're able to specify which Drupal roles can access a particular HTTP verb this means we can open our API to get requests from anonymous users and at the same time we can restrict post patch and delete operations to administrators now that we've got our permissions in order let's try making a few requests to our new API of course before we can make requests to our new API we need to create some content let's just create a simple page node we could also use the Belgian rate to generate sample content for our site after saving our page node or generating some sample content we can switch over to postman to make a request against our new API the URL we need to hit to access our content endpoint is node slash 1 and then add a query parameter for the format we're interested in retrieving in this case let's grab Hal Jason we submit that get requests our API will respond back with a hal json representation of that node it's worth emphasizing here that we've included the request format as a query parameter if we'd rather retrieve xml or just straight json we can simply tweak this query parameter to receive the desired format let's see what node 1 looks like as an XML representation we could also set up our authorization header here to pass our Drupal credentials along to our API request this will allow us to make authenticated request to do things like post nodes let's give that a shot in order to post a new content resource we need to change this endpoint to be entity slash node we also need to set our basic authentication header using our Drupal username and password then we'll set the content type header so Drupal knows to accept how Plus JSON will switch our HTTP verb to post and then we're going to paste in some raw post data this is a little bit obtuse but what we're doing is specifying a link type here so Drupal knows exactly which entity type a node and which bundle an article that we're trying to create after that we provide values for the title field and the body field and we're ready to post our data let's go ahead and send this request to post a node to our Drupal 8 site we're going to need to use a new API endpoint and I apologize for this but a new version of the postman extension as well the endpoint we're going to hit is new blog dot local slash entity slash node we're gonna need to add basic authentication here since only authenticated users are able to make post requests so we'll enter in our username and password and click update request taking a look at the headers that postman is going to add to our request we can see to first there's the content type where we're specifying the type of data we're going to be posting to Drupal in this case how Plus JSON there's also an authorization header that's been added this was created automatically for us when we entered in our authorization information on the previous tab then in the body field we're going to paste in the raw JSON we wish to post to Drupal this is a little bit obtuse how Jason's a little bit hard to type out but what we're doing here is we're specifying a link type so Drupal knows which entity type a node and which bundle an article to create with our data after that we provide values for the title and body and we're ready to post we'll go ahead and click send and if everything goes as expected we should see a 201 created response here if we look at the headers in the response the location we get gives us the URL for our new node in this case node slash 53 back on our site if we load node 53 we can indeed see the test post that we just created with postman so far we've seen it's easy to get and post data from our Drupal 8 site but if we enable the views module we can build an even more powerful API let's build a new view will click on structure and views when we add a new view we're now given a new option for a display type called rest export settings let's create a new view called articles and we're going to show content of type article sorted by newest first and provide a rest export path of API slash articles I'll click Save and edit since this is just a view with an extra display type it works like all of the other views you're familiar with including support for contextual filters let's build a quick view instead of showing the full entity let's just show fields we're going to have the title let's also add a summary field we're going to use the summary or trimmed formatter and limit it to 600 characters and click apply we can see in the preview below that we've just created a JSON response API using views that will just list out the title and a teaser for all of our article nodes without writing a single line of code we'll go ahead and save this view and then take a look at our API slash articles endpoint in post man back in postman we're gonna make a get request to our new API slash articles endpoint here we can see an array being returned with the title and body for each one of our article nodes we just built a new API to export articles using views and just clicking it together without writing any code at all pretty cool huh tarik in this lesson we took a look at how the modules included in Drupal 8 core along with the rest UI helper module allow us to build a simple API to expose content on our site without writing a single line of code we also took a look at the rest pipeline in Drupal 8 and saw various points Drupal modules can provide additional plugins to make our API even more flexible I can imagine contribs as well as supporting additional resource types Drupal 8 truly makes it easy to start building an API for your site so what are you waiting for you
Info
Channel: Drupalize.Me
Views: 19,561
Rating: 4.8558559 out of 5
Keywords: drupal, decoupled, drupalize.me
Id: 4xCndQQVJsg
Channel Id: undefined
Length: 9min 59sec (599 seconds)
Published: Tue Dec 01 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.