如何在powershell中POST到canvas LMS

时间:2016-05-25 13:54:35

标签: json powershell webrequest canvas-lms

我目前正在为大学制作API。这个项目的目的是查看每个课程并检查它是否包含某个模块,如果没有,请添加一个。看起来相当简单,但是让项目变得复杂的是学习语法实际做的事情它!我在powershell中编写此代码,并尝试使用curl并调用Web请求。我试过跟随画布的文档,但我无法得到它。以下是我的两个实例,请您告诉我如何正确格式化。

######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

的文档

更新5-26-2016 我已经弄清楚如何正确格式化消息的正文。现在我收到错误消息

$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

此时我真的不知道该怎么做。此时我们将非常感谢任何指导。

1 个答案:

答案 0 :(得分:0)

经过一周摆弄实际的JSON格式后,我在Invoke-WebRequest上取出了内容类型,看看服务器是否能够猜出格式。它奏效了!