## Web service protocols: REST protocol (with JSON/XML payload support)

webservice_restjson
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.
74 sites
6 fans

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.

#### Installation

Simply place the plugin files in ./webservice/restjson

#### Setup

Should be enabled and setup in the standard way for webservices.  See https://docs.moodle.org/29/en/Using_web_services for details.

#### Usage

The protocol can be called in the usual way for webservices with either JSON or XML data specified in content type (also backward compatible with current REST webservice so will also accept POST form data)

curl -H "Content-Type: application/json" -X POST -d '{"wsfunction":"...", "wstoken":"...",request object}' http://moodle.url/path/to/webservice/restjson/server.php

Parameters such as wsfunction and wstoken can also still be specified in the URL if required

curl -H "Content-Type: application/json" -X POST -d '{request object}' http://moodle.url/path/to/webservice/restjson/server.php?wsfunction=...&wstoken=...

Response format can also be set to XML or JSON by setting the accept HTTP header e.g. to send in JSON and receive in XML (by default same format is used to return as originally received) you could use:

curl -H "Content-Type: application/json" -H "Accept: application/xml"-X POST -d '{request object}' http://moodle.url/path/to/webservice/restjson/server.php?wsfunction=...&wstoken=...

### Contributors

• Fri, Feb 19, 2016, 12:58 AM
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).
• Fri, Feb 19, 2016, 1:20 AM
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.
• Fri, Feb 19, 2016, 2:07 AM
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)
• Fri, Feb 19, 2016, 2:08 AM
Sorry on a mobile on a crowded and somewhat wobbly train. That should have read json not jsonormal.
• Fri, Feb 19, 2016, 6:03 PM
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.
• Fri, Feb 19, 2016, 6:27 PM
OK, I've merged that change and done a new release including this fix plus a fix for the permissions issues.
• Thu, Apr 28, 2016, 10:49 PM
Great plugin. Very handy for any web developer.
• Wed, Mar 22, 2017, 6:19 PM
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
• Wed, Mar 22, 2017, 6:45 PM
Hi Ownen,

I tried this json structure too but no luck {"Courses":[{"fullname":"Bulk insert","shortname":"short bulk","categoryid":1}]}
• Tue, Apr 18, 2017, 8:46 AM
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?
• Mon, Jul 17, 2017, 4:37 PM
( For those who don't know how to send the request via Command Line, like me some minutes ago. )

Because the request format is ambiguous. I couldn't understand how to send the "request Object". so I tried this and it worked :

curl -k -X POST -d "id=63&filename=windiff.zip" "https://localhost/moodle/webservice/rest/server.php?wsfunction=local_wstemplate_hello_world&wstoken=f5b0854a870e05f960cff39fc2a40665"

hope it helps
• Mon, Oct 23, 2017, 7:57 AM
Hello Owen,
I try to use Moodle webservice within google apps, so json could be great to use, but I cant get that plugin to work.
Generally Moodle is not easy platform for anybody. So,
* my Moodle version is 3.1
* permission webservice/rest:use -> allow (standard REST works)
* token is valid (standard REST works)
* plugin webservice_restjson is enabled (0.4 for Moodle 2.9+ 2016021900)
* protocol REST protocol (with JSON/XML input support) -> enabled
request:
curl -H "Content-Type: application/json" -X GET http://elearning.casadelalengua.pl/webservice/restjson/server.php?wsfunction=core_course_get_courses&wstoken=33ff697d305xyz
I've got
{"exception":"webservice_access_exception","errorcode":"accessexception","message":"Wyj\u0105tek w kontroli dost\u0119pu"}
so I don't know what still I have wrong.

Can you help me with this problem?
• Thu, Jul 19, 2018, 6:10 PM
This can be so helpful to web developer
• Wed, Oct 3, 2018, 7:10 PM
Excellent plugin and very helpful for any magento developer.
• Thu, Aug 29, 2019, 3:47 PM
Thanks for this useful plugin!!! It helps me a lot!!!