REST protocol (with JSON/XML payload support)

Maintained by Owen Barritt
REST webservice protocol based on the standard core REST webservice, but with added support for JSON & XML payloads and supports using HTTP ACCEPTS headers for determining response format.
Price option: Free

Supports Moodle 2.9 See all versions
Latest release: 10 years ago
Installations: 153
Downloads (last 90 days): 17

Frankenstyle name: webservice_restjson
Web service protocols

Comments

Comments are no longer open for new posts. Existing comments remain available to read.

Hi Owen, any chance of getting a quick example video for those of us aspiring to use server calls with Moodle but not yet quite up to speed on how they're done?
Hi Ownen,
I tried this json structure too but no luck {"Courses":[{"fullname":"Bulk insert","shortname":"short bulk","categoryid":1}]}
Hi Owen,
How can i format the json object to post, because call core_course_create_courses method for create course, my json structure was [{"fullname":"Bulk insert","shortname":"short bulk","categoryid":1}]
But i got the error link {"exception":"invalid_parameter_exception","errorcode":"invalidparameter","message":"Invalid parameter value detected"}
Could you please suggest what kind of structure i need to send course creation
Great plugin. Very handy for any web developer.
OK, I've merged that change and done a new release including this fix plus a fix for the permissions issues.
Hey Owen, I did some debugging and found the issue what was going wrong with the HTTP_ACCEPT. I made a pull request on it on GitHub.
Sorry on a mobile on a crowded and somewhat wobbly train. That should have read json not jsonormal.
The fact you're getting xml back rather than jsonormal suggests the web service isn't getting your content type header. Try putting the options before the url when you call curl as per the man page (not sure it recognises them afterwards which might be your problem)
Had a quick look at the 'forbidden' message is definitely an error with permissions. It looks to be checking the rest:use permission instead of restjson:use.
Until it's fixed, to get it to work the the REST protocol (with JSON/XML support) needs to be enabled and the webservice user must have the webservice/rest:use permission set to 'allowed'.
The core REST protocol doesn't need to be enabled though and the webservice/restjson:use permission currently doesn't do anything (although it should do instead of the webservice/rest:use one when it's fixed).
The fields are missing error sounds like your not sending the parameters expected by the function you're calling. I'd need some idea of the JSON your sending to look into this as all the JSON I've used has been accepted by the webservice without issue, so I can't recreate this issue at the moment.
Ok my bad, I totally forgot to change the url used in curl from webservice/rest to webservice/restjson. I'm not getting the 403 error (its 200 now) but the response still indicates some thing is error, I'm getting the following XML response:
<?xml version="1.0" encoding="UTF-8" ?>invalidparameterInvalid parameter value detectedNo response types acceptable
For the following curl request:
curl 'http://localhost/moodle/webservice/restjson/server.php?wstoken=26255e91cb715c25cc39ff9cee9d8843&wsfunction=core_user_get_users_by_field' -X POST -d '{
"field":"id",
"values":["2"]
}' -H 'Content-Type: application/json'
I noticed this DEBUGINFO error message is generated in the restjson/locallib.php so I will try to debug that myself (unless you straight away know whats going wrong).
wstoken has my tokenID
Here is my request:
curl 'http://localhost/moodle/webservice/rest/server.php?wstoken=&wsfunction=core_user_get_users_by_field&moodlewsrestformat=json' -X POST -d '{
"field": "id",
"values": [2]
}' -H 'Content-Type: application/json'
The response is 403 'Forbidden'.
I checked my apache logs and its giving me the following [Thu Feb 18 17:18:16.057213 2016] [:error] [pid 6903] [client 127.0.0.1:39224] Debugging: The server died because the web services or the REST protocol are not enable in \n* line 39 of /webservice/rest/server.php: call to debugging()\n
For some reason it is referring to webservice/rest/server.php instead of webservice/restjson/server.php.
What JSON are you sending as a request?
Thanks for your response.
When both protocols / permissions are enabled / allowed then it does not seem to accept my JSON as input, the response is that fields are missing.
It uses the standard REST protocol's code in places, so it might be checking those permissions somewhere to cause the fail. I'll try and have a look when I get a free moment.
Can you access the JSON/XML support version if both protocols are enabled and both permissions are set to allowed?