4

I am trying to register a new user on Drupal7 via services with JSON. Actually I am posting info from JAVA (android) with JSON format to the server with services module enabled. A sample successful JSON post is:

    {
    "name": "erfan",
    "pass": "1234",
    "mail": "erfan@example.com",
    "status": "1"
     }

URL: [siteURL]/[endpoint]/user/register

I was successful creating users with basic info such as name,email,status.

I have added custom fields like field_address, field_phone, etc to the web site and want to send them via JSON to the server.

The problem is I can not figure out how to post these data to the server and couldn't find any documentation about it. My trial and error approach was not successful too, because I am not good in PHP coding. So please help me. Thanks.

  • 1
    Could you edit the question and add one of the attempts? It'll be a bit easier to advise once we've seen the code. Entity fields are structured like $account->field_name[$language][$delta][$column_name] if that helps. An example for a text field might be $account->field_foo[LANGUAGE_NONE][0]['value'] – Clive Jan 10 '14 at 19:00
  • I added more details about my problem. I am not a PHP expert. :) – erfannf2 Jan 10 '14 at 19:43
4

Hope this will help you,

In Drupal 7 you can use the below JSON format to add custom fields(field_user_image)

{
    "name": "test",
    "pass": "password",
    "mail": "test@gmail.com",
    "field_user_image": {
        "und": [{
            "fid": "2"
        }]
    }
}

I have tried in my local and it is working fine.

  • Created new field UUID

    enter image description here

  • Calling the Service using Firefox poster add-on

    enter image description here

  • Code


  { 
    "name": "testuser123", 
    "pass": "testuser123", 
    "mail": "testuser123@gmail.com", 
    "field_register_uuid": 
      { "und": 
        [{ 
          "value": "65C7DCA4-8F03-473C-9E8F-08B3F29E91AE" 
        }] 
      }
  }

I can able to create new user with the custom field

References

  • This doesn't seem to work. I'm using UUID Field, and want to create a user with a UUID, which is just a text field, really. { "name": "test", "pass": "password", "mail": "test@gmail.com", "field_uuid": { "und": [{ "uuid_field": "65C7DCA4-8F03-473C-9E8F-08B3F29E91AE" }] }} drupal.org/sandbox/Mile23/1326218 – paul-m Jul 21 '14 at 19:22
  • I have updated my answer. – inizio Jul 22 '14 at 10:10
  • Thanks. But nope. :-) I suppose it's something to do with the way authentication is happening, or some other configuration problem. – paul-m Jul 22 '14 at 18:00
  • I discovered I can create the user and then use PUT to update the field. The JSON above is correct, but it has to be a 2-step process. drupal.org/node/790416 – paul-m Jul 22 '14 at 18:15
2

This should be a comment, but I haven't enough reputation ...
When you create a user, you can only pass the fields that are shown in the registration form. Otherwise you'll need first to register the user and then update it with the new fields, as @paul-m said in his comment.

To show a field in the registration form go to

configuration->people->account settings-> manage fields
click the edit link of the desired field and check "Display on user registration form."

  • Hi and welcome to Drupal Answers. Your answer in fact does contribute to the original question, so I would recommend you to re-format it and improve it. – Wtower Nov 12 '14 at 16:05
  • At the time of writing this answer probably it was not possible to update custom fields in one go, but now drupal.stackexchange.com/a/156404/11058 answer works for me. – Raj Pawan Gumdal Dec 3 '18 at 19:36
2

The field value may have to be "flattened" in order for the user registration to work. In the example below, the field is a List (text) field with radio buttons for the widget.

  • POST ?q=[my-endpoint]/user/register.json
  • Content-Type: application/json

JSON data to POST:

{
  "name":"bob",
  "mail":"bob@gmail.com",
  "conf_mail":"bob@gmail.com",
  "pass":"secret",
  "pass2":"secret",
  "field_choices":"2"
}

This also works for radio buttons:

"field_choices":{"und":"a"}

However, when we use a select list widget on this same field, the JSON is formed like this:

"field_choices":{"und":[{"value":"2"}]}

The JSON above will not work for radio buttons, you'll get a 406 - Not Acceptable An illegal choice has been detected. Please contact the site administrator. This is a lesson and reminder that the Services module can sometimes be dependant on your field's widget when it comes to the JSON it expects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.