RESTful protocol
Premium Certified Partner
Maintained by
Catalyst IT
A REStful webservice plugin for Moodle LMS
This plugin allows Moodle's webservice interface to operate in a more RESTFul way.
Instead of each webservice call having a URL query parameter define what webservice function to use, webservice functions are made available by discrete URLs.
Comments
Comments are no longer open for new posts. Existing comments remain available to read.
When using nginx as a reverse proxy in front of Moodle, we noticed that header names are automatically normalized to lowercase.
For example, a client request like:
-H "Content-Type: application/json"
-H "Accept: application/json"
-H "Authorization: xxx"
will arrive at the backend as:
-H "content-type: application/json"
-H "accept: application/json"
-H "authorization: xxx"
Therefore, in the webservice_restful plugin, it is important to ensure that header processing logic treats header names case-insensitively, according to the HTTP/1.1 specification (RFC 7230), which defines header field names as case-insensitive.
I suggest updating the code to normalize header keys to lowercase before checking or comparing them, or at least allow case-insensitive matching.
Thanks for considering this!
I am happy to announce that the plugin received the "Moodle Workplace ready" award after being tested and found useful by the Worplace team (MDLSITE-6684). Congratulations!
https://moodle.org/plugins/local_wsgetroles
Using the ootb invocation, I get back a list of roles:
============================================================
curl --globoff ^
-d "wstoken=YmZjNDA5ZDQ4NGM5OWE1OWYxYTlmODg5" ^
-d "wsfunction=local_wsgetroles_get_roles" ^
-d "moodlewsrestformat=json" ^
"http://10.0.0.121/webservice/rest/server.php"
============================================================
But using the following returns nothing:
============================================================
curl --globoff ^
-H "Content-Type: application/json" ^
-H "Accept: application/json" ^
-H "Authorization: YmZjNDA5ZDQ4NGM5OWE1OWYxYTlmODg5" ^
"http://10.0.0.121/webservice/restful/server.php/local_wsgetroles_get_roles"
============================================================
import requests
headers = {
'Accept': 'application/json',
'Authorization': '555',
'Content-Type': 'application/json'
}
body = '{"options": {"ids":[6]}}'
url = 'https: //server/webservice/restful/server.php/core_course_get_courses'
resp = requests.get(url, headers=headers, data=body)
print(resp)
data = resp.json()
print(data)
The issue was the capitalization of the headers in Accept Authorization and Content-Type. If someone else sees this and copies it pay attention to the space in the url so that I wouldn't get denied by the spam server on these comments and change the authorization number and of course not store it in the scripts.
curl -X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H 'Authorization: 555' \
-d'{"options": {"ids":[6]}}' \
"https: //server/webservice/restful/server.php/core_course_get_courses"
But I'm trying to convert that into a python request and keep getting back a 401 response:
headers = {
'content-type': 'application/json',
'accept': 'application/json',
'authorization': '5555'
}
body = {"options": {"ids":[6]}}
url = 'https: //server/webservice/restful/server.php/core_course_get_courses'
resp = requests.post(url, headers=headers, json=body)
data = resp.json
print(data)
Does anybody see what I'm doing wrong here?
http://coderespect.blogspot.com/2017/06/moodle-api-net-wrapper.html
https://github.com/syedrizwanm/Moodle.API.Wrapper
This plugin could be a life saver!!! I plan to use Office 365 Flow to interact with Moodle and having a RESTFUL API is key! Does anyone know if it works with Moodle 3.7?
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
I also encountered a contenttype header error, which I solved by adding this code to webservice/restful/locallib.php, line 164 (get_requestformat function):
if (isset($_SERVER['CONTENT_TYPE'])) {
$requestformat = ltrim($_SERVER['CONTENT_TYPE'], 'application/');
} else {
// Raise an error if content header not supplied.
$ex = new \moodle_exception('notypeheader', 'webservice_restful', '');
$this->send_error($ex, 400);
}
- This function originally expected a HTTP_CONTENT_TYPE header, but it was CONTENT_TYPE in my case (using the sample curl from this page).