Your First RESTful View in Drupal 8

In a continuation from my first post, An Introduction to RESTful Web Services in Drupal 8, I want to explore how Views interacts with REST in Drupal 8.

As many of you already know, the Views module was added to Drupal 8 Core. With RESTful Web Services also in Core, we now have all the tools we need to create highly customizable solutions out of the box.

In this blog post, I will show you how to create a view that returns a list of content in JSON via the REST API. Let’s get started!

First things first, we want to make sure both the REST and Serialization modules are enabled. Then we need to create some dummy content. To do this, grab the latest Drupal 8 development release of Devel module, and enable just the Devel generate submodule. Navigate to Configuration > Development > Generate content, and create a bunch of articles.

Now let’s create our view. We do not need to create a page or block, so uncheck those options to keep things simple.

After creating the view, you’ll notice we only have a Master display. For REST to work, we need to add a REST export display.

On the new REST export display, we need to set a path. This is the URL that will be used by clients to return the contents of the view. I’m using the following structure to keep things organized, but you can use anything you like.

REST export displays have a single output format, called Serializer It converts output into the format the client requests. In other words, when the client calls the URL we set above, they can specify the desired format output. If a format is not specified, Views returns JSON by default. There are 3 formats available: HAL, JSON, and XML. By default the client can request any of these formats, but if you open the Serializer settings dialog (found in the Format section of the view) you can specify which request formats are accepted.

REST export lets you to return either fields or entities. By setting our view to return entities, we'll get the entire serialized node object. And just like any view, selecting fields lets us pick the fields from the entity we wish to return. For the sake of simplicity here, I've opted to return the content title only.

Remember that we’re using Views, and we have access to its awesome features. We can set permissions to access the path, sort results by custom criteria, and limit the number of results. Views caching is still a work in progress, but once its functional we'll also be able to cache results like any other view. It’s brilliant!

Contextual filters work too. For example we could add the content author UID contextual filter, and clients could then append it to the end of the path (e.g., rest/views/articles/1) to filter results by a specific author.All that's left now is to give it a test run. Make sure to save the view, and then try accessing the URL directly in your browser. As we're not specifying a format, JSON is returned by default.

[{"title":"Eu Populus Wisi"},{"title":"Distineo Gemino Sit Sudo"},{"title":"Tation"},{"title":"Blandit"},{"title":"Consectetuer Lucidus Patria"},{"title":"Quae Valde"},{"title":"Ad Jumentum Quibus"},{"title":"Aliquam Jugis Saluto Ymo"},{"title":"Exputo Sagaciter Te"},{"title":"Bene Causa Neo Torqueo”}]

We can change aliases used for fields by opening the field settings dialog. This is useful if we want something user friendly and don't want to use default field labels.

I recommend using the Dev HTTP Client chrome extension for testing REST APIs. It will allow you to set the Accept header to return something other than JSON, and it formats the output to assist with debugging. It’s also worth noting that you can only use GET. Views won’t accept POST, PUT, PATCH, or DELETE operations.

I hope this was helpful for those of you who are excited about RESTful Web Services in Drupal 8 Core. Building a REST API for an application has always been tricky. Hopefully with the inclusion of REST and Views in Drupal Core, we’re one step closer to standardizing the process.

In a future blog post, I will dive into the OAuth Contrib module and how we can use it to improve the authentication security of our REST APIs. As always, feel free to leave comments and questions below. Thanks!

Related Topics: 

Comments

Hi, I just wanted to see how the custom resources (especially the POST method) works in the Drupal 8 RESTful services. Let's say i have a custom table 'xyz' and i just wanted to create a RESTful resource which will GET and POST data to these tables. Can you please show me some pointers in this?
You will have to create a new entity that defines the schema of this table. See this article for more: https://drupal.org/node/2192175
<p>Yes, just as Alf said or if you want to stick with your custom table you would need to create a &#39;resource plugin&#39; like the <strong>rest</strong> module in core does for entities out of the box. There isn&#39;t much documentation out about this at the moment but there is a couple of slides on the subject by Lin Clark that might prove useful. http://lin-clark.com/d8-rest-slides/#36</p>
Hello. Thank you for this great article ! But I have a strange problem : "/rest/views/articles/" works perfect but when I add the nid aflet I have an error ("rest/views/articles/1" : http://i.imgur.com/RaFLCWC.png). I have another error when I edit export_REST : http://i.imgur.com/qqre2oe.png. Last thing, when I try to access "/entity/node/1", I have a 404, is it normal ? Thank you again for this nice article !
<p>You might want to make sure you&#39;re on the lastest alpha version of Drupal 8. I&#39;ve tested this article using Alpha 11 and it&#39;s working for me. To filter by Node ID using&nbsp;<strong>rest/views/articles/&lt;nid&gt;&nbsp;</strong>you will need to add the <em>Content: Nid</em>&nbsp;contextual filter to the view. Also, in a recent Drupal 8 core update the REST URIs for accessing nodes was changed so you should be able to access via <strong>/node/&lt;nid&gt;</strong>. If you haven&#39;t already, I&#39;d recommend reading&nbsp;<a href="http://drupalize.me/blog/201401/introduction-restful-web-services-drupal-8">An Introduction to RESTful Web Services in Drupal 8</a>.</p>
Hey, thanks for this article. I do have problems with the contextual filters though. As you wrote I have selected `/rest/thing/&lt;nid&gt;` as a path. But how can I use this variable in the Filter section? I tried 'Content: Nid (= &lt;nid&gt;)' but this is not working. (I get a 200 but an empty result set. If I set 'Content: Nid (= 1)' I get a result)
<p>You don&#39;t need to add this to the Filters section. Contextual Filters are filters themselves. Just make sure you have &quot;Content: Nid&quot; set up to take its value from the URL path. Other than that you&#39;re free to set it up as you please. There is more information on contextual filters in the drupal.org documentation:&nbsp;https://drupal.org/node/1578558</p>
Hello! Is there any way to get JSON from exposed filter result? Something like: user filters data in view, and I need to get the result of the filter in JSON?

Add new comment