REST users :: Email address is incorrect

Hi! I’m trying to create a user with an email of the form of first_name.last_name@mydomain but i get

{"error":{"code":500,"message":"Internal Server Error: Error creating","0":"Email address  is incorrect"}}

the request is on the form of:

{ "request_data": [
 {'id': str(new_id), 'ref': str(new_id), 'statut': '1', 'status': '1', 'employee': '1', 'admin': is_admin, 'pass_crypted': None,
 'email': email, 'login': login_name,  'lastname': last_name, 'firstname': first_name }
] }

any idea what i could be doing wrong?
Thanks a lot!

Try with a different email address to validate that you can actually create users

so, even after i fix my code (the dictionary should be sent as json string), no matter how the email is written i get the same error…

are you willing to share your dolibarr version and the json you feed into it?

The correct syntax for version 17.0.2 is as follows:
(may be also on the newer versions…)

{
"email": "your_email@FQDN", 
"login": "login_name", 
"lastname": "last_name", 
"firstname": "first_name", 
"gender": "man", 
"lang": "DE_de"
}

These fields cannot be set by api call:
'pass_crypted', 'pass_indatabase', 'pass_indatabase_crypted', 'pass_temp', 'api_key'

Dolibar version is latest 19.0.2
the the json is this:

info_dict = {'id': str(new_id), 'ref': str(new_id), 'statut': '1', 'status': '1', 'employee': '1', 'admin': is_admin, 'gender': 'man', 'lang': None, 'array_languages': None,
'email': email, 'personal_email': email, 'email_oauth2': None, 'login': login_name, 'lastname': last_name, 'firstname': first_name }

info_dict_str = json.dumps(info_dict)
return { "request_data": [ info_dict_str ]}

Thanks a lot for looking into this!

What is the value of the email variable?

it is of the form first_name.last_name@domain

This looks a bit like Python, are you using Dolibarrpy from dolibarrpy · PyPI ?

The source code can be found here GitHub - JonBendtsen/dolibarrpy: Python module for interacting with Dolibarr API

yes python but i use just the REST API and requests

and domain is a fully qualified domain that the Dolibarr server can lookup?

Does it work if you str(email)

yes, it is the same domain as of the server
no, does not work … what i actually do is:

def hr_post(path:str, data: dict):
    if not path or not data: return None
    ACTION = f'{HR_URL}{path}'
    return requests.post(ACTION, data = data, headers = headers, verify = False, cert = cert_data)

def mk_user_dict(first_name, last_name, email, is_admin):
    new_id = nr_users + 1
    login_name = f'{first_name.lower()[0]}{last_name.lower()}'
    info_dict = {'id': str(new_id), 'ref': str(new_id), 'statut': '1', 'status': '1', 'employee': '1', 'admin': is_admin, 'gender': 'man', 'lang': None, 'array_languages': None,
                 'email': str(email), 'personal_email': str(email), 'email_oauth2': None, 'login': login_name, 'lastname': last_name, 'firstname': first_name }
    info_dict_str = json.dumps(info_dict)

    return { "request_data": [ info_dict_str ]}


myuser = mk_user_dict('XXX', 'YYY', 'XXX.YYY@MYDOMAIN', '1')
pprint(myuser)

result = hr_post('users', myuser)
pprint(result.text)

From the manual of json_decode()

Example #3 common mistakes using json_decode()
<?php

// the following strings are valid JavaScript but not valid JSON

// the name and value must be enclosed in double quotes
// single quotes are not valid 
$bad_json = "{ 'bar': 'baz' }";
json_decode($bad_json); // null

// the name must be enclosed in double quotes
$bad_json = '{ bar: "baz" }';
json_decode($bad_json); // null

// trailing commas are not allowed
$bad_json = '{ bar: "baz", }';
json_decode($bad_json); // null

?>

→ exchange ' with " see my example

BTW: id, ref, status are not taken into account. pass_crypted cannot be set by api call.
see ~/htdocs/user/class/api_users.class.php in public function post(...) and ~/htdocs/user/class/users.class.php in public function create(...) and update(...)

hmm, so i realized that request_data has the value of an array of strings ! so i have to do this:

def mk_user_dict(first_name, last_name, email, is_admin):
    login_name = f'{first_name.lower()[0]}{last_name.lower()}'
    info_dict = { "firstname": first_name, "lastname": last_name,  "login": login_name, "email": str(email), "statut": "1", "employee": "1", "admin": is_admin }

    print(json.dumps(info_dict))

    whole_dict = { "request_data": [ json.dumps(info_dict) ]}
    whole_dict_str = json.dumps(whole_dict)
    return whole_dict_str

but as a consequence the string that i use as payload looks like this:
the actual data (dumped to string)

{"firstname": "XXX", "lastname": "YYY", "login": "XYYY", "email": "XXX.YYY@DOMAIN", "statut": "1", "employee": "1", "admin": "1"}

but then when i dump everything to string again (to send as payload)

{"request_data": ["{\"firstname\": \"XXX\", \"lastname\": \"YYY\", \"login\": \"XYYY\", \"email\": \"XXX.YYY@DOMAIN\", \"statut\": \"1\", \"employee\": \"1\", \"admin\": \"1\"}"]}

Is there a way to debug the php (php-fpm in this case) to see what actually is the problem?
Thanks a lot!

I don’t think php-fpm is the problem.
Remove the {"request_data": [ at the beginning and the trailing "]}

You can use the inside syslog-function in api_users.class.php in the function post(…) ala:

dol_syslog(get_class($this)."::post ".$this->useraccount->$field." value: ".$value, LOG_ERR);

insert before $this->useraccount->$field = $value;

and look at the results in dolibarr.log if you have the debug-module activated.

Here a screenshot from the Swagger UI


→ user “XYYY” is successfully created

1 Like

OMG, i spent so much time on this!!! THANK YOU!!!
The problem was that in my API explorer i got this: