我想在我的PUT
中使用REST API
方法,我想我有语法问题。到目前为止,这就是我所拥有的:
$url3 = "example.com"
$contentType3 = "application/json"
$basicAuth3 = get_token
$headers3 = @{
Authorization = $basicAuth3
};
$data = @{
userId = "_39_1";
courseId = "_3_1";
availability = {
available = "Yes"
};
courseRoleId = "Student"
};
$json = $data | ConvertTo-Json;
Invoke-RestMethod -Method PUT -Uri $url3 -ContentType $contentType3 -Headers $headers3 -Body $json;
我不认为Invoke-RestMethod
能够读取$json
变量,这就是为什么它给我一个错误。有什么建议?
我得到的错误如下:
Invoke-RestMethod:远程服务器返回错误:(400)错误请求。在E:\ PowerShell \ Get_User_Enroll.ps1:62 char:1 + Invoke-RestMethod -Method PUT -Uri $ url3 -ContentType $ contentType3 -Headers $ he ... + ~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidOperation :( System.Net.HttpWebRequest:HttpWebRequest)[Invoke-RestMethod],WebException + FullyQualifiedErrorId:WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
最好,
您必须为可用性创建另一个哈希表。您在可用性对象的@
之前错过了{
。
$url3 = "myurl";
$contentType3 = "application/json"
$basicAuth3 = get_token
$headers3 = @{
Authorization = $basicAuth3
};
$data = @{
userId = "_39_1";
courseId = "_3_1";
availability = @{
available = "Yes"
};
courseRoleId = "Student"
};
$json = $data | ConvertTo-Json;
Invoke-RestMethod -Method PUT -Uri $url3 -ContentType $contentType3 -Headers $headers3 -Body $json;