You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Moodle REST API创建用户时已传users却提示缺失参数的求助

解决Moodle REST API创建用户时"Missing required key in single structure: users"错误

以下是排查和解决的关键步骤:

1. 调整请求体格式(最可能的原因)

Moodle的REST API对POST请求的参数格式有特定要求:它默认期望使用application/x-www-form-urlencoded编码传递参数,而非直接发送JSON请求体。你之前的Python脚本大概率是将用户数组的JSON字符串作为users表单字段的值发送,而非直接把{"users": [...]]作为请求体。

具体操作:

  • 在Postman/n8n中,将请求的Content-Type设置为application/x-www-form-urlencoded
  • 添加表单字段:
    • 键:users
    • 值:用户数组的JSON字符串(注意不要在外层包裹{"users": ...}),示例值:
      [
        {
          "username": "testusername",
          "password": "Welcome1!",
          "firstname": "testfirstname",
          "lastname": "testlastname",
          "email": "testemail@example.com",
          "idnumber": "JSHL",
          "description": "Active",
          "auth": "manual",
          "lang": "en"
        }
      ]
      

2. 验证Web服务权限

确认你的令牌对应的外部服务已授权core_user_create_users函数:

  • 进入Moodle后台 → 网站管理 → 插件 → Web服务 → 外部服务
  • 找到你配置的服务,点击“添加函数”,检查core_user_create_users是否已在列表中

3. 检查字段合法性

虽然当前报错是缺失users参数,但可以排除字段格式问题:

  • lang字段:Moodle部分版本不支持en_us格式,尝试改为en或en-US
  • password:确认符合Moodle的密码策略(比如长度、字符要求)

4. 确认请求方法与URL参数

  • 确保请求方法是POST(而非GET)
  • 检查URL中的moodlewsrestformat=json拼写正确,wstoken有效

内容的提问来源于stack exchange,提问作者Recypher

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 15:44:57