Skip to main content

I’m new to the Docebo API and I am trying to get the API to add a user.

I can add a user via the “API Explorer” using these fields:

{
"userid": "killer@domain.com",
"email": "killer@domain.com",
"password": "some_random_stuff",
"firstname": "Killer",
"lastname": "Cat",
"force_change": 1,
"level": 6,
"send_notification_email": true,
"select_orgchart": {
"375": 1
}
}

I’m using php to connect to the API, and submitting these fields there via the following construct:

$datap"userid"] = "killer@domain.com";
$data "email"] = "killer@domain.com";
$data "password"] = "some_random_stuff";
$data "firstname"] = "Killer";
$data "lastname"] = "Cat";
$data "force_change"] = 1;
$data "level"] = 6;
$data "send_notification_email"] = true;
$data "select_orgchart"] = array("375" => 1);

// Build a POST request
$opts = array(
'http' => array(
'method' => 'POST',
'header' => array(
'Authorization: Bearer '.$token,
'Content-Type: application/x-www-form-urlencoded\r\n',
),
'content' => http_build_query($data),
'ignore_errors' => true
)
);
$context = stream_context_create($opts);

// Poll the API
$get_response = file_get_contents($url, false, $context);

The URL I am using is "/manage/v1/user".

I get this  response from the php request:

{

"name":"Bad Request",

"message":m

{"field":"userid","message":"You haven't filled in some mandatory fields"},

{"field":"password","message":"You haven't filled in some mandatory fields"}

],

"code":1002,

"status":400

}

This suggests that there are two fields missing… but clearly I have filled in both the userid and password fields.

FWIW - I get the same response if I use php curl to run the POST request.

Any ideas what I might be doing wrong?

Any and all suggestions / help would be most welcome.

Thanks!

@JCD  I haven’t coded in PHP for years, but from what I see (here) the http_build_query function generates a URL-encoded query string.

For the Docebo API you need to send a JSON-formatted message.

Change the Content-Type and check the json_encode function.


@alekwo Boom! Indeed that’s what is was - with json encoding the API requests are working perfectly.

Thank you very much for your insightful and speedy reply. Much appreciated.


Reply