Invoke-RestMethod REST API PUT方法

时间:2017-03-27 16:08:29

标签: powershell

我正在尝试使用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)Bad Request。   在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

最佳,

1 个答案:

答案 0 :(得分:7)

您必须为可用性创建另一个哈希表。您错过了可用性对象@之前的{

$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;