Using Drupal's Views as a JSON Web Service with the REST Server

Drupal 6 does not have web services built in - instead they are part of the Services module. The Services module enables your Drupal installation to provide data to clients such as Flash, Javascript AJAX clients, or in my case, an iPhone application.

To use the Services Module, you will have to install additional servers to communicate with clients - for instance, the REST Server and the JSON Server. Using Three20’s GitHub example as a guideline, I can see that JSON parsing is a lot simpler on the iPhone than XML, and I’d prefer to stay away from XML anyway. There is also a native iPhone plist server for Drupal, but at some point I may create an Android client, and there’s no reason to limit myself to iPhone only.

This led me to the JSON Server as the solution, but that turned out to be a dead end. I’ve had much frustration with the JSON Server - from what I can tell, it really doesn’t work. I’ve gotten lots of 404 errors and not much else. From its Issues on, I’m not the only one with JSON Server issues. Luckily the REST Server offers a JSON interface as well, by just appending “.json” to the end of the GET URL.

Getting JSON representations of individual Drupal nodes with the REST Server is very straight forward - make a GET request to http:///services/rest/node/.json where is 1.

Some gotchas here are that even if you don’t care about securing access to that node (because it’s public content on your Drupal site anyway), you need to enable Key authentication for your services in Drupal and then turn it off in the Services Settings.  You also need to remember to enable the Node Service module, and ensure that the “load node data” permission for the node_service module is allowed for anonymous and logged in users (if you want it to be public, of course). Any of these things not being done properly can lead to a 404 error when you make the REST call.

This all works well for loading one node at a time, but I’d like to show information from multiple nodes on one iPhone screen. And I’d like to make that show up as quickly as possible - so that means that rather than making multiple HTTP calls to Drupal over the network, I’d like to just make one call to the server.

Drupal’s views are perfect for this - views are nothing more than a layer on top of SQL, and what I want is basically a few fields from each node reference on my main node. The nice thing here is that I can develop and debug my view from Drupal to make sure it outputs the data that I want first, and then simply load that view into the iPhone app and work with the data there. But to do that I need to have a little chunk of middleware that connects the iPhone app to the Drupal view - this is where the Views Service module comes in. Enable Views Service, and the Services admin settings will show a views.get method. Using the admin interface, you can experiment with the arguments needed to get your view to work as a service. Of course, this is not an endpoint you can actually use as a service, but just an admin tool.

To actually load a view using the REST Server, I had to do some serious research. The best source of information was an issue for the Rest Server where the developer answered - another question. Unfortunately his answer included a typo that threw me off for a while, so here’s what you actually need to do -

POST to /services/rest/service\_views/get.json

with the arguments ‘view_name’ and ‘args’ - you can test these from the admin interface. Using curl, the request looks like this from the command line:

curl -verbose --data 'view\_name=**your\_view\_name**' --data 'args=\[**your\_args\_1,your\_args\_2**\]' http://**your\_server\_name**/services/rest/service\_views/get.json

Let me know if this helps you out with the REST Server, Views, and JSON - this was a tricky subject to google for.