Last updated May 13, 2015. Created on December 6, 2012.
Edited by likewhoa, martinjbaker, jeff h, Chi. Log in to edit this page.

RESTWS exposes Drupal resources (e.g. entities) as RESTful web services. The module makes use of the Entity API and the information about entity properties (provided via hook_entity_property_info()) to provide resource representations.

It aims to be fully compliant to the REST principles.

Installing the Module

  • Copy the whole restws directory to your modules directory (e.g. DRUPAL_ROOT/sites/all/modules) and activate the RESTful Web Services module.
  • Use Drush commands: drush en restws
  • There is no user interface to configure.

Usage / Testing

To obtain the JSON representation of an entity use your browser to visit: http://example.com/<entity type name>/<entity id>.json
For example: http://example.com/node/1.json or http://example.com/user/1.json would resolve as a JSON dump to client. There are no "service endpoints" to configure as resources are just available at uniform paths like "node/1", "user/1". One can use a client in the form of a browser plugin to test out the different CRUD functions.

Filtering / Querying

You can filter for certain resources by passing parameters in the URL, for example to retrieve all nodes of a certain node type, or all taxonomy terms in a specified vocabulary.

Query format:

HTTP GET /<entity type name>.<format>?<filter>=<value1>&<meta_control>=<value2>

Examples:

/node.json?type=article will return all "article" nodes
/taxonomy_term.json?vocabulary=7 will return all terms in vocabulary 7

See the module README.txt for further details.

Permissions

In order to access entities via this interface, permissions must be granted for the desired operation (e.g. "access content" or "create content" for nodes).
Additionally each resource is protected with a RESTWS permission that can be configured at "admin/people/permissions#module-restws".

Additional Resources

Browser Plugins & Software for Testing

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

teenage’s picture

I just post a small "how to" on the Restws authentication method in the following issue :
http://drupal.org/node/1913358
It could be added to the restws_auth_basic readme ?

Neograph734’s picture

The preferred authentication methods (for D8) are described here: https://drupal.org/node/2076725. Note that in Drupal7 a similar POST request has to be made to /user BUT, the form_id for D7 is: "user_login".

We've assumed that the basicauth module and the token were both needed, similar to what teenage explains. However as klausi explains (https://drupal.org/node/2152325), that it's in fact not the correct way.

So you either use the base64 method and send credentials on every request. Or you POST once against /user with credentials and catch the cookie. From there you can use the token.

kriyar’s picture

Dear all,

I tried to create new user by sending data from client website to resource website (installed RESTful Web Services and basic auth module), but the result "406 Not Acceptable: Entity property created doesn't support writing". Both (client and resource) are Drupal 7 websites. Here is my code in client site:

<?php
$user_info
= array(
   
'name' => 'username222',
   
'pass' => user_password(8),
   
'init' => 'username222@yahoo.com',
   
'status' => 1,
   
'mail' => 'username222@yahoo.com',
   
'created' => time(),
   
'timezone' => variable_get('date_default_timezone', '')
);
$json = json_encode($user_info);
$result = add_new_entity($json);
print_r($result);

function
add_new_entity($post_data) {
 
$base_url = 'http://domainname' ;
 
$ch = curl_init();
 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
 
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
 
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
 
curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
 
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, TRUE);
 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 
curl_setopt($ch, CURLOPT_URL, $base_url . '/user/login');
 
curl_setopt($ch, CURLOPT_POST, TRUE);
 
curl_setopt($ch, CURLOPT_POSTFIELDS, "name=restwsuser&pass=password&form_id=user_login");
 
curl_setopt($ch, CURLOPT_URL, $base_url . '/restws/session/token');
 
$HTTP_X_CSRF_TOKEN = curl_exec($ch);
 
$headers[] = 'Content-type: application/json';
 
$headers[] = 'X-CSRF-Token: ' . $HTTP_X_CSRF_TOKEN;
 
curl_setopt($ch, CURLOPT_URL$base_url . '/user');
 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
$response = curl_exec($ch);
 
curl_close ($ch);
  return
$response;
}
?>

Anyway, when I tried to create a node (I just change node data structure), it works properly.

So any thing wrong with my above code or user data structure is not correct?

I really appreciate for your help.

Kriyar

Program Developer at Zaman University, Cambodia
HP: (+855) 15 843 149

kriyar’s picture

It's working now, just change user data structure.

<?php
$user_info
= array(
   
'name' => 'newuser',
   
'status' => 1,
   
'mail' => 'newuser@yahoo.com',
);
?>

Program Developer at Zaman University, Cambodia
HP: (+855) 15 843 149

mahesh.umarane’s picture

Hello,
i am new for RESTws. can anybody guide me how to create custom webservices using restws?
i want to create webservice for different content types.

rajivranjan09’s picture

Let me start by telling you what an amazing module this is. Its so easy to install and use that you quickly forget that it is a separate module and not part of the core Drupal 7 functionality.

I have a drual 7 instance where I create content using the Drupal UI and then another application of mine uses the restws module to export the content as xml, parse the xml and display data. Recently, I started internationalizing my website and now have a need to import the translated xml files back into the drupal system. It worked fine for the content that did not have images in them. However for content that had some text and images, I have not been able to get the content import functionality to work. Here is a sample of what I am trying to import -->

URL --> http://localhost:8888/mydrupal/node

HTTP Method --> POST

Headers in request --> Content-Type: application/xml, X-CSRF-TOKEN: xxxxxxxxxxxxxx, Encoding: UTF-8

<?xml version="1.0" encoding="UTF-8"?>
<result>
   <type>fileloadtest</type>
   <title>A file load page</title>
   <body>
      <value>&lt;p&gt;SA_This is a page with an image.&lt;/p&gt;</value>
      <summary />
      <format>filtered_html</format>
   </body>
   <field_photo>
         <und is_array="true">
             <item>
                 <fid>62</fid>
                 <uid>1</uid>
                 <filename>raranjan_1.jpg</filename>
              <uri>http://localhost:8888/mydrupal/en/file/62</uri>
              <filemime>image/jpeg</filemime>
          </item>
      </und>
   </field_photo>
   <language>sa</language>
   <author resource="user" id="1">http://localhost:8888/mydrupal/en/user/1</author>
</result>

Response I get --> 406 Not Acceptable: Invalid data value given. Be sure it matches the required data type and format.

Somebody had suggested using "data" as the root node but that did not work either. Would appreciate if someone would help me with this.

_Rajiv

sarindufit’s picture

Hi,

I followed the instructions given in this article and set up the REST API for the Drupal 7.31. The only difference is that i enabled the plugin through the UI without using the Drush commands.

When an API method is invoked, it gives the error "404". Both Entity and the REST Web Service modules are added and enabled. I doubt, this issue is due to a permission error. When sending a request as the administrator, how is the authentication done? Should i pass the credentials according to the basic http authentication.

Please provide support to resolve this issue.

Thank you in advance.

druajay’s picture

Hi, i am new about the Restful web services and how do i start to implement web services in my site.

pkosenko’s picture

I don't see that there is any way to implent JSONP export for remote linkage. "?callback=jsonpcallback" results in an error -- "callback" parameter is unkown. Is there any way to do JSONP? If so, any documentation?

Peter Kosenko

adrian1231’s picture

Is there any url query that can be used to order the results retunred by creation date in decending order?

What I am really looking for something similar to this "mysite.com/node.json?type=[type-name]&sort=created&orderBy=dec".

p.s. Thank you for the great module!! It has really made my life a whole lot easier.