Invoke-RestMethod REST API PUT方法

问题描述 投票:1回答:1

我想在我的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

最好,

powershell
1个回答
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;
© www.soinside.com 2019 - 2024. All rights reserved.