Drupal User Registration via REST API Service Module - An Example and Possible Roadblocks

Services Module - Registration

Services Module is a standardized solution for building API's so that external clients can communicate with Drupal. It provide Drupal plug-ins that allow others to create their own authentication mechanisms, request formats, and response formats. Some of the existing users of this module may have faced the service registration issues while implementing it, I have worked out some solutions for various registry issues you might come across.

Technical details of the tools used.

  • Drupal 7.41
  • Module: Service [ Version:- 7.x-3.13 ]
  • Chrome Extension: Advanced REST client - 4.12.8-stable.0

Register Users through service API module:

  • URL: myproject.com/apiendpoint/user/register
  • Method: POST

CASE 1 – Using default registration parameters

{

"name" : "testuser",

"mail" : "test@gmail.com",

"pass" :{ "pass1" : "123456", "pass2" : "123456" }

}

 

This should get you going!

CASE 2 – Custom registration parameters

 

{

"name" : "testuser",

"mail" : "test@gmail.com",

"pass" : { "pass1" : "123456", "pass2" : "123456" }, "field_voucher_number" : { "und": [{ "value": "1234" }] }

}

you need to add one more line of code mentioned here in the bold.

If the response is 200 OK then, everything's alright and user has registered successfully.

EXCEPTION No. 1.

If you are getting 500 internal server error as shown in the screen-shot below, you need to follow below mentioned procedure to make user register.

Services Module - Registration 

First of all check error report logs. If you have same error as shown in below screen-shot then you need to apply patch #6.

Link to the patch - https://www.drupal.org/node/2543010

Services Module - Registration 

After that again try to register. if you haven't implemented captcha you will get a response as shown in the screen-shot below.

Services Module - Registration 

Captcha Exception

If you have captcha validations on registration page then you will get error named “406: Not Acceptable: What code is in the image? field is required.” as shown in the screen-shot below

Services Module - Registration 

Solution

To rectify this issue we will need to apply one form alter hook which will be applied only when registration will be performed through API.

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {

if($form_id == 'user_register_form'){

if (arg(0) == 'api') {

// You might have to update this line depending on the path to your API unset($form['captcha']);

}

}

}

After this user registration will work properly for the given fields as shown in the screen-shot below.

Services Module - Registration 

Custom field with text

If you are adding new custom field involving “text”, you can use below mentioned method.

{ "name" : "testuser", "mail" : "test@gmail.com", "pass" : { "pass1" : "123456", "pass2" : "123456" }, "field_test_text" : { "und": [{ "value": "1234" }] } }

Also there's one more thing that you need to be careful about:

Suppose you have added “field_test” as per shown in the image and have added its list value for radio button list are as described below:

a|A

b|B

c|C

and add field value to store while registration is

{ "name" : "testuser", "mail" : "test@gmail.com", "pass" : { "pass1" : "123456", "pass2" : "123456" }, "field_test" : { "und": "a" } }

this will throw an error as per shown in the below image.

Services Module - Registration 

Now, let's try something else!

Lets create new field “field_test2” and set its field type and widget as shown in the image, I have set its value as described below:

1|a

2|b

3|c

now add field parameter in the registration api:

{ "name" : "testuser", "mail" : "test@gmail.com", "pass" : { "pass1" : "123456", "pass2" : "123456" }, "field_test2" : { "und": 1 } }

this works perfectly as shown in the below image

Field type “List(text)” generates some issue to store values and it is not added in the error logs either.

So, basically if you are using “String” to store its value, it will throw an error, so instead use “integer” value and it will work perfectly!

Difference between “field_test” and “field_test2” is shown in the below image

Services Module - Registration 

Stay connected for more technical blogs. You can even drop us your any Drupal Web Development queries, we provide optimal solutions.