如何在 powershell 中 POST 到 canvas LMS

How to POST in powershell to canvas LMS

我目前正在为一所大学制作API。这个项目的目的是查看每门课程并检查它是否包含某个模块,如果不包含,则添加一个模块。看起来很简单,但使项目复杂化的是学习语法以实际执行它!我在 powershell 中编写这段代码,我尝试使用 curl 并调用 web 请求。我尝试遵循 canvas' 文档,但无法获取。这是我遇到的两个例子,你能告诉我如何正确格式化它吗?

######this is the first way I tried it and the error I get Invoke-WebRequest : {"message":"missing module parameter"}
$temp2=($mainURL+$course.ID+"/modules"+$securityToken)
$reply = curl -Uri $temp2  -Method Post -Body '{"module[name]"="Getting started","module[position]"="1"}'
#######

#########This is the second way I've tried and the error I get Invoke-WebRequest : The remote server returned an error: (422) Unprocessable Entity.
$url="https://University.instructure.com/api/v1/courses/1371/modules?access_token=abc123"
$body= "{'module[name]'='Getting started,'module[position]'=1}"
Invoke-WebRequest -Uri $url -ContentType "application/json" -Method Post -Body $body
#########

来自网站的文档https://canvas.instructure.com/doc/api/index.html

2016 年 5 月 26 日更新 我已经想出如何正确格式化消息的正文。现在我收到错误消息

$header = @{"Authorization"="Bearer "+ $security_token}
$body=  '{"module[name]":"Getting started","module[position]":"1"}'
$curlly=Invoke-WebRequest -Headers $header -Body $body -Method Post -ContentType "application/json"  -Uri ($url_main+"courses/1371/modules")   
$module = ConvertFrom-Json $curlly.Content    
curl : Invalid URI: The hostname could not be parsed.
    At line:1 char:1
    + curl
    + ~~~~
        + CategoryInfo          : NotSpecified: (:) [Invoke-WebRequest], UriFormatException
        + FullyQualifiedErrorId : System.UriFormatException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

此时我真的不知道该怎么做任何指导将不胜感激。

经过一个星期摆弄实际 JSON 格式后,我取出了 Invoke-WebRequest 上的内容类型,看看服务器是否能够猜出格式。成功了!