Drupal Services Examples

Category: 

jDrupal: Be sure to check out jDrupal for an easy to use JavaScript Library to communicate with Drupal Services.

This document describes how to read/write entities to/from Drupal using the Services module. It also covers how to handle session authentication. Let's get started...

Installation and Setup

  1. Download the Services Module
  2. Enable It
  3. Drink Dr. Pepper

Now to create a service, we'll start with the basics... go to Structure -> Services -> Add, and enter some stuff like this:

Namemy_services
Endpoint titleMy Services
ServerREST
Path to endpointmy_endpoint

Ignore the other fields for now. Click 'Save' to, you guessed it, save your new service.

Next up, we need to enable some Resources for our Service. Go to Site Structure -> Services -> List, then click 'Edit Resources' next to your Service.

Check the boxes next to each resource that you'd like to enable, save, then flush all of Drupal's caches.

Examples

Each example below includes the HTTP method (GET, POST, PUT, or DELETE) and an example URL to use. When using POST or PUT, you must send along some data, and that example data will be listed on the line after the http method and url. If there is a special Content-Type header to use, it will be mentioned after the data, otherwise just use application/json.

C.R.U.D.I. = Create, Retrieve, Update, Delete, Index

X-CSRF-Token

For all POST, PUT and DELETE requests (except for user login), you must attach a token to the header of your request. The name of the header to use is:

X-CSRF-Token

The value of the token can be retrieved here:

http://localhost/drupal-7?q=services/session/token

Content-Type

Unless otherwise noted, the Content-Type header of your request should be set to:

application/json

Session Authentication

GET: http://localhost/drupal-7/?q=services/session/token

POST: http://localhost/drupal-7/?q=my_endpoint/system/connect.json

POST: http://localhost/drupal-7/?q=my_endpoint/user/login.json
username=w5TmhqOvWF&password=4mBj5tYQu8
application/x-www-form-urlencoded
    
POST: http://localhost/drupal-7/?q=my_endpoint/user/logout.json

POST: http://localhost/drupal-7/?q=my_endpoint/user/register.json
{"name":"yFzqH7dNEv","mail":"kT3miidPAx@example.com","pass":"MSQ4JvRhZh","pass2":"MSQ4JvRhZh"}
application/x-www-form-urlencoded

User C.R.U.D.I.

POST: http://localhost/drupal-7/?q=my_endpoint/user.json
{"name":"LSAwug2XxL","mail":"soRkkjZisJ@example.com","pass":"iPpNcwnEqW","pass2":"iPpNcwnEqW"}

GET: http://localhost/drupal-7/?q=my_endpoint/user/52.json

PUT: http://localhost/drupal-7/?q=my_endpoint/user/52.json
{"uid":"52","mail":"8dGyPlGSrB@example.com","current_pass":"iPpNcwnEqW"}

DELETE: http://localhost/drupal-7/?q=my_endpoint/user/52.json

GET: http://localhost/drupal-7/?q=my_endpoint/user.json

Node C.R.U.D.I.

POST: http://localhost/drupal-7/?q=my_endpoint/node.json
{"type":"article","title":"BEVVtDcZmL","language":"und"}

GET: http://localhost/drupal-7/?q=my_endpoint/node/125.json

PUT: http://localhost/drupal-7/?q=my_endpoint/node/125.json
{"node":{"nid":"125","title":"KHrkDnudKR","language":"und"}}

DELETE: http://localhost/drupal-7/?q=my_endpoint/node/125.json

GET: http://localhost/drupal-7/?q=my_endpoint/node.json

Comment C.R.U.D.I.

POST: http://localhost/drupal-7/?q=my_endpoint/comment.json
{"subject":"S5KeJTVFED","comment_body":{"und":[{"value":"gSXNfHPxgf"}]},"nid":"126"}

GET: http://localhost/drupal-7/?q=my_endpoint/comment/30.json

PUT: http://localhost/drupal-7/?q=my_endpoint/comment/30.json
{"subject":"wULr64gdpj","comment_body":{"und":[{"value":"YhmemEQ4Cy"}]},"cid":"30"}

DELETE: http://localhost/drupal-7/?q=my_endpoint/comment/30.json

GET: http://localhost/drupal-7/?q=my_endpoint/comment.json&parameters[cid]=30

Taxonomy Vocabulary C.R.U.D.I.

POST: http://localhost/drupal-7/?q=my_endpoint/taxonomy_vocabulary.json
{"name":"WkSTeYfABe","description":"RQoHVs22yZ","machine_name":"wksteyfabe"}

GET: http://localhost/drupal-7/?q=my_endpoint/taxonomy_vocabulary/16.json

PUT: http://localhost/drupal-7/?q=my_endpoint/taxonomy_vocabulary/16.json
{"vid":"16","name":"gSXktVuVty","machine_name":"wksteyfabe"}

DELETE: http://localhost/drupal-7/?q=my_endpoint/taxonomy_vocabulary/16.json

GET: http://localhost/drupal-7/?q=my_endpoint/taxonomy_vocabulary.json&parameters[name]=WkSTeYfABe

Taxonomy Term C.R.U.D.I.

POST: http://localhost/drupal-7/?q=my_endpoint/taxonomy_term.json
{"vid":"17","name":"V8m22sqRBO"}

GET: http://localhost/drupal-7/?q=my_endpoint/taxonomy_term/13.json

PUT: http://localhost/drupal-7/?q=my_endpoint/taxonomy_term/13.json
{"vid":"17","tid":"13","name":"C5Kjy2WOxK"}

DELETE: http://localhost/drupal-7/?q=my_endpoint/taxonomy_term/13.json

GET: http://localhost/drupal-7/?q=my_endpoint/taxonomy_term.json&parameters[vid]=17&parameters[name]=V8m22sqRBO