简体   繁体   中英

How to POST in powershell to canvas LMS

I am currently making an API for an university. The purpose of this project to to look at every course and check if it contains a certain module and if not, add one in. Seems fairly simple enough, but the thing that is making the project complicated is on learning the syntax on to actually do it! I am writing this code in powershell and I have tried to use curl and invoke web request. I tried following canvas' documentation, but I cannot get it. Here are two instances I have, can you please show me how to properly format this.

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

Documentation from the website https://canvas.instructure.com/doc/api/index.html

UPDATE 5-26-2016 I have figured out how to properly format the body for the message. Now I am getting the error message

$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

I really do not know what to do at this point any guidance will be appreciated at this point.

After a week of fiddling around with the actual JSON format, I took out the content type on the Invoke-WebRequest to see if the server would be able to just guess the format. It worked!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM