【发布时间】:2020-07-23 01:53:57
【问题描述】:
根据https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-7#example-2--run-a-post-request,我正在尝试调用一个简单的 POST 方法,但出现一些错误。
我的指示是:
$uri = "https://localhost:44355/api/job/machine-status";
#$machineName = HOSTNAME.EXE;
$machineName = "simPass2";
$body = @{
Name = $machineName
Status = "Complete"
}
Invoke-RestMethod -Method 'Post' -Uri $uri -ContentType 'application/json' -Body $body;
我的错误是
Invoke-WebRequest : Unable to connect to the remote server
At line:8 char:1
+ Invoke-WebRequest -Uri $uri -Method Post -ContentType 'application/js ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : System.Net.WebException,Microsoft.PowerShell.Comman
ds.InvokeWebRequestCommand
【问题讨论】:
-
端口是否开放?你能
curl吗? SSL 证书是自签名的吗?也许您必须跳过证书验证? -
我认为您的
$body不是有效的 json。你可能需要在那里做-Body ($body | ConvertTo-Json) -
@timur 谢谢这是我问题的答案!
-
@timur 我认为您应该将其发布为答案,尽管 OP 自己已经这样做了。那时你可能仍会获得赏金..
-
@timur 我同意,你应该获得赏金!
标签: powershell powershell-3.0 invoke-restmethod