Skip to main content

Registering a user via REST is an important process particularly when the requests come from a mobile device.

In working through the process you might have come across error message like:

403 Forbidden : "Only anonymous users can register a user."

422 Unprocessable Entity : "A Password cannot be specified. It will be generated on login."

So how did I get these and what to do?

First off, check the REST set up in the administration area of Drupal.  Configure User and User registration in the Configuration > Web Services > REST.  Knowing that there is a big difference between User and User registration.  

User registration

User registration will allow an anonymous user to register and have a password sent via email to their email client.  The pathway to this resource is 

/user/register?_format=hal_json

If you use this method to achieve registering a user, then the above error messages will occur.  The user/register method is used to register an anonymous user.  When creating a user from an app this is not ideal.  From an app, the best method is to use user method.

User

Check that you have enabled this resource.  This can be achieved in the same pathway as noted above

Configuration > Web Services > REST

To access this resource use /entity/user?_format=hal_json 

The body of this call will contain as an example

{"_links":{ "type":{"href":"https://example.com/rest/type/user/user"}},
"name":[{"value":"sarah"}],
"pass":[{"value": "sarah1"}],
"mail":[{"value":"sarah@example.com"}],
"field_firstname":[{"value":"Sarah"}],
"field_lastname":[{"value":"Appleman"}]
}

Okay the password is so not secure... I'll leave that for you to change.  One this to note if you pass this as above, the user will be blocked.  So an administrator will need to change their status.  Otherwise to avoid this and have them active on registration add the status parameter to the body

"status":[{"value": "1"}]

 

Related articles

Andrew Fletcher04 Apr 2025
Managing .gitignore changes
When working with Git, the .gitignore file plays a critical role in controlling which files and folders are tracked by version control. Yet, many developers are unsure when changes to .gitignore take effect and how to manage files that are already being tracked. This uncertainty can lead to...
Andrew Fletcher26 Mar 2025
How to fix the ‘Undefined function t’ error in Drupal 10 or 11 code
Upgrading to Drupal 10.4+ you might have noticed a warning in their code editor stating “Undefined function ‘t’”. While Drupal’s `t()` function remains valid in procedural code, some language analysis tools — such as Intelephense — do not automatically recognise Drupal’s global functions. This...
Andrew Fletcher17 Mar 2025
Upgrading to PHP 8.4 challenges with Drupal contrib modules
The upgrade from PHP 8.3.14 to PHP 8.4.4 presents challenges for Drupal 10.4 websites, particularly when dealing with contributed modules. While Drupal core operates seamlessly, various contrib modules have not yet been updated to accommodate changes introduced in PHP 8.4.x. This has resulted in...