Passing json array as a parameter in a Rest API

Passing json array as a parameter in a Rest API

by Mustafa Hajjar -
Number of replies: 3

I am trying to make a rest call to a Microsoft calendar service.  A generic curl call works however when trying to use the Moodle core Rest API I get an error saying: "clean_param() can not process arrays, please use clean_param_array() instead." from the rest.php, but I have to pass the array as shown below.

I tried json_encode for the parameters that are arrays but that didn't work either.  I tried changing the interface for the service: 

public function get_api_functions() {
     return [
          'add' => [
               'endpoint' => 'https://graph.microsoft.com/v1.0/me/events',
               'method' => 'post',
               'args' => [
                    'subject' => PARAM_RAW,
                    'body' => [
                         'contentType' => PARAM_RAW,
                         'content' => PARAM_RAW
                      ],
                    'start' => [
                         'dateTime' => PARAM_RAW,
                         'timeZone' => PARAM_RAW
                      ]
                    'end' => [
                         'dateTime' => PARAM_RAW,
                         'timeZone' => PARAM_RAW
                      ],
                    'location' => PARAM_RAW,
               ],
          'response' => 'json'
          ],
     ];
}

I tried:
          $service->call('add', $params);
and
          $service->call('add', $params, true);
and
          $service->call('add', $params, json_encode($params));

Any help is greatly appreciated.

Best,
Mustafa

Average of ratings: -
In reply to Mustafa Hajjar

Re: Passing json array as a parameter in a Rest API

by Rone Santos -
Picture of Plugin developers
I have the same problem, did you find a solution?
In reply to Rone Santos

Re: Passing json array as a parameter in a Rest API

by Mustafa Hajjar -

It's been such a long time but the service works now:

// set event parameters
$params = [
'summary' => $event->name,
'description'=> $event->body,
'start' => json_encode(array('dateTime'=>$event->startDateTime->format('Y-m-d\TH\:i\:s'),'timeZone'=>'UTC')),
'end' => json_encode(array('dateTime'=>$event->endDateTime->format('Y-m-d\TH\:i\:s'),'timeZone'=>'UTC')),
];

// set event raw parameters
$rawparams = $params;
$rawparams['start'] = array('dateTime'=>$event->startDateTime->format('Y-m-d\TH\:i\:s'),'timeZone'=>'UTC');
$rawparams['end'] = array('dateTime'=>$event->endDateTime->format('Y-m-d\TH\:i\:s'),'timeZone'=>'UTC');

// call the calendar rest api
try {
$service = calendar_rest($client);
$result = $service->call('add', $params, json_encode($rawparams));
} catch (\Exception $e) {
throw $e;
}

In reply to Rone Santos

Re: Help

by Edeval Ap Zaghetti -
Boa Tarde Rone

Se puder me dar um Help, eu coloquei todos os escopos e quando vou salvar a reunião no moodle aparece a seguinte mensagem

Exceção - 403: Request had insufficient authentication scopes.



O que poderia ser este erro
Edeval 
edevalapzaghetti@gmail.com
19-984563590

Attachment erro.png