After you have installed the Services module and created a service, you can test whether it is working by enabling a resource and retrieving that resource. During initial testing, it will make your life easier if you disable all authentication. You can enable the authentication later after you have confirmed that the service you have created is returning results as expected.

For the purpose of this test, we will assume that you have created a service named "test_service" with an endpoint of "test" and a REST server. We will also assume that you have not yet enabled any authentication. If you edit your service at /admin/structure/services/list/test_service you should therefore see something like this:

Using the node resource for testing is a good way to get started because creating nodes is easy in Drupal and because the Services module provides the full range of CRUD methods for nodes: create, retrieve, update, delete (and index).

1. Make sure anonymous users have permission to view nodes

Since you have not enabled authentication, Services has no way of associating your request with a user ID and will treat all requests as coming from an anonymous user. In order to retrieve node information, therefore, the "View published content" permission must be enabled at admin/people/permissions.

2. Figure out the path for your node resources

The next thing you'll need to do to test your resource is figure out what path it lives under. This is put together from several pieces of information you entered into the Services admin screens. The formula is

http://<your_domain>/<your_endpoint_path>/<your_resource_path>

Additionally, you can specify the response format that you want returned by adding a period followed by the abbreviation for that response format to the end of the URL. If no response format is specified, XML is is the default (assuming that your service has XML enabled).

Your endpoint path was defined when you followed the steps to "add a new endpoint." (See Services Installation and Setup.) To get your resource path, edit your endpoint, and click the "Resources" tab as shown in the screenshot below. Each available resource will be listed, with boxes checked for each resource that you have enabled. Look for the "node" resource and see if anything is entered for its "alias." If no alias is entered, then the name of the resource will be the resource path. Otherwise, the alias will be the resource path.

3. Test the node retrieve and node index methods

The Services module's RESTful interface uses the HTTP GET method to submit "retrieve" and "index" requests, so they are the easiest REST queries to test directly from your web browser. To test the "create" methods you will need to use an HTTP POST method. To test the "update" method will require an HTTP PUT method, and the "delete" method requires HTTP DELETE. It takes a little more effort to put together POST, PUT and DELETE methods, so we'll start by testing some "retrieve" and "index" requests.

Examples: Let's assume that you want to get an list of nodes your resource using the endpoint path defined above as "test." and you want to retrieve node resources. If you want the result to come back formatted as XML, the URL for a "index" request to retrieve a list of nodes would therefore be:

http://<your_domain>/test/node.xml
or just:
http://<your_domain>/test/node

If you want the result as JSON, the URL for this request would be:

http://<your_domain>/test/node.json

To get a single node with node id of 5, the URLs above would become:

http://<your_domain>/test/node/5.xml
or
http://<your_domain>/test/node/5
or for JSON:
http://<your_domain>/test/node/5.json

If you visit the URLs listed above in your browser, you should receive responses in XML and JSON as specified. If you get a Page Not Found error, you have something wrong.

Once you have confirmed that your request is returning the result you expected, go ahead and test other node resource queries. The results that come back will be limited by the permissions you have enabled for anonymous users. For example, if you turn off the "View published content" permission for anonymous users, you will get a response of "Access denied for user anonymous" when you try the URLs above.

4. Testing node creation, updating and deletion

As noted above, HTTP methods other than HTTP GET are needed to create, update and delete resources via a RESTful interface. A number of plug-ins are available that make it easier to test the HTTP PUSH, HTTP POST and HTTP DELETE methods via different web browsers, including the following:

Browser tools

Code samples

A number of documentation pages have been created with code samples that show how to implement requests to Services, including the following:

Each example assumes a specific choice for the format in which a request is submitted, which affects request parsing, as well as the format for the response that gets sent back to the requester. Further information about those choices can be found in the documentation for Services response formatters and request parsing.

Drupal modules

  • The HTTP Client module provides a client library for Drupal that is designed to work with Services.
  • The Drupal_http_request() function, which is part of Drupal core, also provides functionality for submitting HTTP requests.

Other tools

See Also

Comments

xgi’s picture

Followed this procedure... when I got blocked on step 3.
The HTTP status was 404... even though the content (node list, ...) was delivered.

This is not an issue when called from a browser (since content get displayed)
... but when the service is called from another app that check HTTP status before processing, this is a real problem.

As I'm rather new to Drupal, it took me while to figure out what was happening:
how could the service return the expected content... with a 404 status?

I finally found it out: the mod_rewrite was not enabled in my apache installation !!
(I could see this because I was also not able to activate "Clean URLs').
My understanding is thus:
REST URL point to an unexisting resource (at least from apache point of view)
... which gets rerouted to drupal server (as all 404 get displayed with drupal itself)
but drupal had no problem to bind the request to my service... and provide the good content.
Result: the expected result, with a 404 HTTP status!!

Once mo_rewrite configured (see http://drupal.org/node/15365), it works like a charm!!

... hope this comment can save some time to other drupal starters...

Thanks a lot to for all you already did (and still do)...

simplexm5’s picture

Your answer finally helped me solve my problem after 3 hours of searching.

dongtian’s picture

It is very help when I forgot to turn on "View published content" permission for anonymous users.

tkuldeep17’s picture

Saved a lot of time mine..

tkuldeep

skolesnyk’s picture

REST server url doesn't have to end with .json.

beerbomber’s picture

I spent hours figuring out that the Path to endpoint needs to be something like this

aaa/bbb/ccc

I was getting all kind of 404 when I setup my endpoint with

/aaa/bbb/ccc, or
aaa/bbb/ccc/

I wanted to have my endpoint to be
rest/api/1