Hello,
How do we create a new user on the drupal site using services 3 module using REST and Curl from the external php form.
I mangaed to login into the site from the external php form , But not able to create a new user.
I tried with http://example.com/rest/user/register (REST server url) and passes the user data (name , email, pass) , But while submitting the external php form i am getting this error "The requested URL returned error: 406" . I searched a lot about the error but not able to find the result , One thing i know that the error is coming because of passing wrong parameters .
Any help will be appreciated.
Comments
Comment #1
ygerasimov CreditAttribution: ygerasimov commentedI would recommend to use user create method for this. Please use test as a reference about curl options and how arguments should be prepared http://drupalcode.org/project/services.git/blob/refs/heads/7.x-3.x:/test...
Feel free to reopen this issue if have any other questions.
Comment #3
drupaldrop CreditAttribution: drupaldrop commentedHello ygerasimov, thanks for the reply , i have follow the test document and try to pass the same parameters , but still i am getting this error "406Auth error The requested URL returned error: 406". Please help me in this. Here is my code.
Thanks
- Akash
Comment #4
ygerasimov CreditAttribution: ygerasimov commentedif you send your data as json, then you should encode your arguments with json_encode. So
$user_data = http_build_query($user_data);
should be
$user_data = json_encode($user_data);
Also mention json formatter in your call. So instead of
$request_url = $services_url . '/user';
Do
$request_url = $services_url . '/user.json';
Comment #5
drupaldrop CreditAttribution: drupaldrop commentedHello Yegerasimov, I have made the changes that you have stated , But still i am getting the same error 406 Auth Error.Do permissions has to do something with this?
now my service url is -> http://localhost/test/trunk/rest/user.json
I have one question in mind in My case we are not defining the name of the services like user.register or node.create but we are depending on the parameters we are passing , so Services know what action to take on the drupal site?.
- Akash
Comment #6
ygerasimov CreditAttribution: ygerasimov commentedyes, you may have problem with permissions. Please make sure when you create a user, you have permission 'administer users'.
Services understand the method you are calling by request type:
POST create
GET retrieve
PUT update
DELETE delete
In your case you do POST request, so services understand it properly.
If you do action (like user/register) you should do POST query to http://localhost/test/trunk/rest/user/register.json
Comment #7
drupaldrop CreditAttribution: drupaldrop commentedHello Ygerasimov, I tried with the above post query but no luck . So what I did I used this example(http://drupal.org/node/1334758) to test if any other services i am able to call or not . But now i am getting this error "401 Unauthorized: Wrong username or password, or user doesn't have right permission." .I am quite sure about the username and password i am passing is correct , cause i am using administrator credentials , I also gave permission "administer users" to anonymous but still not luck. Also I read README.txt in the services module. I checked the services lib folder and it contains the file spyc.php file which is the required file to use REST server. I also noticed one thing about services authentication module which has a dependencies over "Oauth common" module and this module is not available for drupal 7. so i cant see what service authentication module is for?
Comment #8
drupaldrop CreditAttribution: drupaldrop commentedI installed OAuth module and check the session authentication checkbox in the services administer page now i am able to create a node and can login to the site easily, Now i am moving to register a user on the site ..... X
Comment #9
drupaldrop CreditAttribution: drupaldrop commentedHello ygerasimov, I am sending user data in below structure.
$user_data = array(
'name' => 'new123',
'email' => 'ab12345@gmail.com',
'pass1' => 'test1',
'pass2' => 'test1',
'status' => 0,
);
But when i print the response of the curl , only the email field in the user registration get refilled with above data , i know the structure is wrong that i am passing, but can't figure out till now the correct structure.
please help.
Comment #10
ygerasimov CreditAttribution: ygerasimov commentedwhen you register you should send:
array('name' => 'new123', 'mail' => 'new123@email.com', 'pass' => 'password123').
See http://drupalcode.org/project/services.git/blob/refs/heads/7.x-3.x:/test... for example.
Comment #11
drupaldrop CreditAttribution: drupaldrop commentedThanks Ygerasimov, Finally i am able to register the user on the site.Below is the working code for registering user on the site.
Now i moving to include a membership on the user registration page , So when user registered on the site some payment will get deducted from his account, I am using membership suite module to include membership on the user register page and through services i am going to fill the form including membership ......X
Comment #12
ygerasimov CreditAttribution: ygerasimov commentedCongrats! Marking this issue fixed.
Comment #14
arcane CreditAttribution: arcane commentedBuilding on the example provided above, I added required fields to the user account, which display on the user registration form in Drupal 7. How would I go about pre-populating these fields in the user data array? Is this allowed?
Sample Required field
I tried add this to the user data array, but it didn't work when I tried to to add a user through the script:
Comment #15
arcane CreditAttribution: arcane commentedin sites/all/modules/services/resources/user_resource.inc, I don't see any code that would map form state values for added fields (unless I'm wrong?)
Comment #16
aklump CreditAttribution: aklump commentedI wanted to share my experience with getting this to work.
First point learned:
I was going round and round getting a 406, when I realized that I had some required fields on our registration form.
If you have any required fields, you must send those in your post array.
Second point learned
Even though all required fields were being sent I still got a 406... why? Mollom. We have mollom set up on our registration form and this leads to a 406 (I'm not sure yet how to get around this). When I disabled mollom the 406 disappeared and the user was created.
Captcha form elements, which are required, must be filled in, or disabled.
How to send field values
I thought I'd paste the array I'm sending, which shows how to send fields
Comment #17
aklump CreditAttribution: aklump commentedFollow up post to #16: How I got around mollom when creating users through the API call.
In the authentication function I set a session var:
$_SESSION['my_module']['active_api_call'] = TRUE
Then in a hook_form_alter I test for that variable if true and then hide mollom form elements:
Comment #18
kaido.toomingas CreditAttribution: kaido.toomingas commentedWhat I learned..
You can inspect user register page and these values what you gonna submit are these values what you see when you inspect name attribute.
So
would come
values => name=myusername, mail=mymail@mail.com, conf_mail=mymail@mail.com, status=1 (status 1 will make user logged active).
And now (Drupal 6) post these values to http://somedomail.com/?q=api/user/register.json
And now (Drupal 7) post these values to http://somedomail.com/api/user/register.json
Comment #19
mstrelan CreditAttribution: mstrelan commentedAnother way to disable mollom:
Can be called any time after mollom_init(), so doesn't have to be specifically in a form alter.
Comment #20
mstrelan CreditAttribution: mstrelan commentedAlso, to those struggling to send field data...
Instead of
I am using
Comment #21
Anonymous (not verified) CreditAttribution: Anonymous commentedI got the result in core php using curl function but need to check using poster or postman
$account['field_first_name[und][0][value]'] = 'Gamma'; how to set this value in poster application
parameter section
field_first_name[und][0][value] = 'Gamma' still i get
this error
field_name_first][und][0][value":"First Name field is required
Comment #22
Hajdarevic CreditAttribution: Hajdarevic commentedYour comments above are very helpfully and I have made a custom php script with which my visitors are able to register through a custom form.
After that I have created a custom user profiles pages where all fields of currently logged user are automatically filled... Now I want to find a way to update those fields with PHP cURL,
So, that means when user go to custom user profile page he/she will be able to change lets say 'e-mail', 'city', 'country' and so on and click Submit. After that fields needs to be updated...
Any kind of help will be appreciated.
Thanks
Comment #23
raj45 CreditAttribution: raj45 commentedI was getting the 406 error when trying to create a new node, which had a required field, which didn't have a default value. After setting a default value, it now works as designed.
Comment #24
damien_th CreditAttribution: damien_th as a volunteer commentedHere is my code to create an user using Rest Services coming OOTB from Drupal:
Prerequisites:
Here is my Query:
POST your.drupal.website/entity/user?_format=hal_json
Headers:
Body:
Comment #25
Chris CharltonUgh. #20 was right for me today. Odd. And undesirable.
Anyone have a tip on how to make this cleaner?
Comment #26
jordiserra CreditAttribution: jordiserra commentedI tried #24 and it doesn't work for me. I'm trying to call /entity/user rest service from D7, but it keeps me saying Access denied, no matter how I try to access. I tried using basic auth with a specific D8 user, with super user credentials, without credentials (as anonymous user)...
However, I can create nodes and taxonomy terms using /entity/node and /entity/taxonomy_term endpoints.
Any ideas about what am I doing wrong?
Thanks
EDIT: Answering myself, finally I solved my problem and it was not related to the process explained in #24. It was a problem with a field, giving, as a result a not valid entity.