Monday, August 3, 2015

Working with RESTful Web Services Drupal 7 module

Whoever worked with Drupal 7 CMS probably felt a need to expose his web location resources somehow. And we all heard about Services module with plenty of tutorials on-line about setting all those parameters, endpoints and others. However, there is another, publicly available, easy-to-use module called RESTful Web Services which relies on Entity API module and its powerful methods of exposing and interchange internal Drupal resources.
So, I wanted to expose couple of entities (content types) via REST web API, and after I red all about this tiny module it seemed that it was the right choice. After all, I didn't want anything fancy, just to be able to do classic CRUD ops via exposed service, and that is just what RESTful WS is supposed to do - complying with best REST practice, right?
It is worth to mention that my setup was somehow different than the usual scenario in which you would use the module. Namely, I was running Drupal 7 instance under fresh OpenBSD httpd web server which looked kind of simple, but promising in terms of getting the job done. One of the key issues that I found with httpd is that it is missing a lot of features that contemporary web servers are including by default, one of them being clean URLs, or URL rewriting if you wish. As you can imagine, that means only one - no JSON format delivery via extension (such as http://resource/node/1.json), but I needed to create custom header pairs such as Accept:application/json and Content-type:application/json. It worked! Now I could use some library such as JSON.NET, deserialize what I am pulling with System.Net.WebClient.DownloadString(), map that with my custom class and use it wherever and whatever I want for.

RESTful WS may be an old module that lost some of its proscribed functionality over the years, but it is still good at serving JSON or XML to the third party clients. Upcoming Drupal 8 comes with REST in it's core, so there will be no need for manual installation of modules for the same functionality. Until then, many of the Drupal 7 sites could be served by this handy little thing.