3

I want to create an API request in Postman that creates a category in Moodle. The following are the steps which I did in Moodle :

  1. Enabled Web service
  2. Created an External service and added core_course_create_categories function.
  3. Enabled REST protocol
  4. Created Token.

This is my POST URL: http://localhost/test/moodle/webservice/rest/server.php?username=admin&password=Password@1&service=mycustomservice&name=CustomCategory&parent=1&idnumber=CSTM1&description=Desc

Under Authorization tab, I have selected Bearer Token and added the Token from Moodle web service.

When I send the request, I am getting invalidtoken Invalid token - token not found error.

Can anyone please suggest a solution for this

Moodle version: 3.4.8 and Postman version 7.6.0

Thanks in advance.

Sarath
  • 89
  • 2
  • 9

1 Answers1

5

You can not call API by GET Method. In postman you have to call the API using POST method. pass your token in form-data as wstoken. call your function as wsfunction.

Check my Screenshot below. enter image description here

Sonil Gandhi
  • 179
  • 11
  • Hi. I tried that. But displayed an error "invalidparameter Invalid parameter value detected". Please check the screenshot https://cdn1.imggmi.com/uploads/2019/9/6/e3f0957a364f455516fdb5ef7f7ba402-full.png – Sarath Sep 06 '19 at 14:01
  • Hey, You are very close to your solution. You just have to pass categories data in array formate. I have updated my screenshot in the answer I have provided, please check it. – Sonil Gandhi Sep 07 '19 at 14:45
  • Hey, It worked. I had to remove "userid" from the body to make it work. Please check the new screenshot: https://cdn1.imggmi.com/uploads/2019/9/7/59fd14d7f33940ade575ce229dea5ffb-full.png. Also, I have noticed that you have included moodlewsrestformat in both Params and Body tab which I have not included. Is it required in the Params tab? – Sarath Sep 07 '19 at 16:26
  • @Sarath moodlesrestformat is not required, actually, this parameter is used in old moodle version. For API functions and how to pass its parameter, you can refer http://your_moodle_url/admin/webservice/documentation.php If my answer helped you please mark it correct or do upvote it. Thank you. – Sonil Gandhi Sep 07 '19 at 19:17
  • Also, can you please tell me how to pass parameters using raw Json format. I tried to pass the parameters as raw Json format. But it's throwing an error(invalidtoken Invalid token - token not found). Please check the screenshot below: https://cdn1.imggmi.com/uploads/2019/9/9/43025ba27f7ea20d05af38c6d674df09-full.png – Sarath Sep 09 '19 at 16:08
  • There is no way in Moodle you can pass payload in raw json format. You can use 3rd party moodle plugin to achieve this. Bellow is a plugin which provides thing which you want, I didn't use it but you can give it a try. https://moodle.org/plugins/webservice_restjson – Sonil Gandhi Sep 10 '19 at 06:20
  • Ok. I have created a POST request to enroll a user into a course which is working perfectly. Please see the screenshot below: https://cdn1.imggmi.com/uploads/2019/9/11/8414d090c8382c49a60c9a42493702cd-full.png. Could you please tell me how to enroll multiple users in a single POST request? – Sarath Sep 11 '19 at 13:45