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.

Hello Owen, I was happy to find this plugin such that I could send REST request with JSON formats. However I seem to have trouble getting the plugin to work (the default REST protocol provided in Moodle is working with problems). The setup is as following:
- Moodle 2.9+
- I installed the Plugin (version 0.2), in the plugins overview it is marked as enabled.
- In External protocols I have only REST protocol (with JSON/XML input support) enabled.
- I have a 'webservice' role where webservice/restjson:use is set to 'Is allowed'
If I now run my CURL statement, I get a 403 response and a 'Forbidden' message.
Once I switch from REST protocol (with JSON/XML input support) to just the default REST Protocol and set the webservice/rest:use to 'Is allowed' for the role. It works fine again (except for not being able to send JSON as request).
Would you have any idea what is going? Or tips to see where it might go wrong? Thanks!

Hi Owen. Sorry for the confusion. You are right indeed. The default REST protocol accepts requests with content-type form-urlencoded, whereas yours adds support for native JSON/XML content-type requests, as you have described. I did not realize that and I was incorrectly referring to response only.

Thanks for clarification. I am happy to approve this plugin now. I think we can leave this live as a contributed plugin for now, no need to submit it as a core patch (that suggestion was based on my wrong assumptions).

It might help to add some example usage of the protocol in the description here, say something like curl -H "Content-Type: application/json" -X POST -d '{"wsfunction":"...", "wstoken":"..."}' http://moodle.url/path/to/webservice/restjson/server.php or whatever is needed and would illustrate the basic usage of the protocol.

Thanks again for sharing your plugin. You are cleared to land, welcome to the Plugins directory!

I'm happy to be corrected if I'm wrong, but the current REST server won't accept requests in JSON or XML formats, only as form data. The only JSON/XML support is in the response sent.
Needed a webservice that accepted requests in JSON format, which is the main reason I wrote this. Given the number of forum posts asking about json requests I thought this might be useful to others too so shared here.
If it seems more appropriate to submit this as a patch to the main rest protocol, am happy to do so (may not be until next week now though). Where would I submit this? Here still or somewhere else?

Hi Owen. Thanks for sharing the pluging with the Moodle community.

I did a quick look at the difference between your protocol and the default rest protocol that ships with Moodle. To be honest, I don't quite understand the reasons why a new protocol is needed. As far as I know, the default rest protocol does support both XML and JSON payloads. It seems to me the only unique feature of your plugin is that is changes the default response format from XML to JSON if the client claims it accepts JSON and does not accept XML.

Do I get it right? If so, such a change would better be submitted as a patch the Moodle core REST protocol as it seems to be reasonable behaviour.

Looking forward to hear from you on this topic. Meanwhile, I'll mark this as needing more work to give us time to discuss the intentions properly. Thanks for understanding.