1

I'm trying to create a user via the REST API. I was able to do it successfully using the REST Client Firefox plugin, but when I actually try to call the URL via code, I get no response. No errors, just nothing. I'm trying to debug the PHP (I am a PHP developer), but cannot for the life of me find the point where the code starts.

I'm doing something like this:

$('#download_trial').click(function() {
            // also need to create email; can we call rest/user/register from here?
            var name = $('#mail').val();
            $.ajax({
                url:site_url + 'rest/user/token.json',
                type:"POST",
                contentType:"application/json; charset=utf-8",
                dataType:"json",
                success: function(data){
                    mytoken = data.token;
                     var data = {
                                "mail":name,
                                "account":{
                                   "name":name,
                                   "pass":generatePassword(),
                                   "mail":name
                                }
                             };

                    $.ajax({
                        url:site_url +'rest/user/create.json',
                        type:"POST",
                        data:data,
                        contentType:"application/json; charset=utf-8",
                        headers: { "X-CSRFToken": mytoken },
                        dataType:"json",
                        success: function(data){
                            console.log("created user");
                            console.log(data);
                        }
                      });
                }
              });

        });

The rest/user/token.json call is working correctly; I'm able to console.log the token that's returned. But from there, it doesn't seem to be hitting the create.json function as far as I can see.

I've tried adding a watchdog statement to the _user_resource_create function in user_resource.inc, but it never seems to be getting there. What is the initial entry point for the PHP when rest/user/create.json is called, so that I can try debugging it via my IDE debugger?

FYI, this is so that a visitor (non-authenticated user) can create a user account on our system from an external system. Do I need any unusual permissions to allow this?

  • 1
    Not an answer to the question, but I think services is looking for a header named X-CSRF-Token (not X-CSRFToken) if that helps – Clive Apr 24 '15 at 19:00
  • I'll see if that makes a difference; when I worked with Django it was spelled without the extra hypen, so that's habit. – EmmyS Apr 24 '15 at 20:18
  • Fixed the token header, but unfortunately it doesn't seem to have made any difference. Still not getting anything back, and isn't hitting the create function that I can see. – EmmyS Apr 25 '15 at 2:20
  • Do a POST to '?q=rest/user.json` instead, and send along something like { name: 'bob', mail: 'bob@hotmail.com' }. Or just use the user_register() function provided by jDrupal. – tyler.frankenstein Apr 25 '15 at 16:08
  • I'm not familiar with jDrupal; will look into that. Thanks. – EmmyS Apr 25 '15 at 18:44

Your Answer

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

Browse other questions tagged or ask your own question.