使用Python调用Moodle REST API复制课程报错:缺少courseid参数
问题:调用Moodle REST WebService复制课程时提示缺少courseid参数
使用Python调用Moodle的core_course_duplicate_course接口复制课程时,代码无法正常运行,返回参数缺失错误。
原代码
import requests import json token = 'mytoken123' domainname = 'https://example.com' functionname = 'core_course_duplicate_course' # Course ID of the course to be duplicated courseid = 61 # Replace with your course ID # New course details newcoursedata = { 'fullname': 'New Course Full Name', 'shortname': 'NewCourseShortName', 'categoryid': 10, 'visible': 1, } restformat = 'json' # Also possible to use 'xml' data = { 'courseid': courseid, 'options': newcoursedata, } headers = { 'Content-Type': 'application/json', } # REST CALL serverurl = f'{domainname}/webservice/rest/server.php?wstoken={token}&wsfunction={functionname}&moodlewsrestformat={restformat}' response = requests.post(serverurl, headers=headers, data=json.dumps(data)) # Print response print(json.dumps(response.json(), indent=4))
返回的错误信息
{ "exception": "invalid_parameter_exception", "errorcode": "invalidparameter", "message": "invalid parameter value (Missing required key in single structure: courseid)", "debuginfo": "Missing required key in single structure: courseid" }
问题原因及解决方法
错误的核心原因是Moodle REST WebService默认不支持直接接收JSON格式的POST请求体,你当前用JSON传递参数的方式不符合Moodle的参数解析逻辑,导致系统无法识别courseid参数。
方法一:改用表单格式传递参数(推荐)
表单格式是Moodle REST API最兼容的参数传递方式,几乎所有版本都支持。嵌套参数通过[key]语法表示:
import requests import json token = 'mytoken123' domainname = 'https://example.com' functionname = 'core_course_duplicate_course' courseid = 61 newcoursedata = { 'fullname': 'New Course Full Name', 'shortname': 'NewCourseShortName', 'categoryid': 10, 'visible': 1, } restformat = 'json' # 构造表单格式参数,嵌套字段用[]标识层级 form_data = { 'courseid': courseid, 'options[fullname]': newcoursedata['fullname'], 'options[shortname]': newcoursedata['shortname'], 'options[categoryid]': newcoursedata['categoryid'], 'options[visible]': newcoursedata['visible'], } serverurl = f'{domainname}/webservice/rest/server.php?wstoken={token}&wsfunction={functionname}&moodlewsrestformat={restformat}' # requests会自动设置正确的Content-Type,无需手动指定 response = requests.post(serverurl, data=form_data) print(json.dumps(response.json(), indent=4))
方法二:使用JSON格式(需Moodle版本支持)
部分新版本Moodle支持JSON格式请求,但需要将所有参数包裹在params键中:
import requests import json token = 'mytoken123' domainname = 'https://example.com' functionname = 'core_course_duplicate_course' courseid = 61 newcoursedata = { 'fullname': 'New Course Full Name', 'shortname': 'NewCourseShortName', 'categoryid': 10, 'visible': 1, } restformat = 'json' # 将所有接口参数包裹在params键下 data = { 'params': { 'courseid': courseid, 'options': newcoursedata, } } headers = { 'Content-Type': 'application/json', } serverurl = f'{domainname}/webservice/rest/server.php?wstoken={token}&wsfunction={functionname}&moodlewsrestformat={restformat}' response = requests.post(serverurl, headers=headers, data=json.dumps(data)) print(json.dumps(response.json(), indent=4))
内容的提问来源于stack exchange,提问作者Madamadam
相关产品推荐
相关产品推荐

