在Windows PowerShell 3.0中引入了 Invoke-RestMethod cmdlet。
Invoke-RestMethod cmdlet 接受 -Body<Object>
参数以设置请求主体。
由于某些限制,Invoke-RestMethod cmdlet 在我们的情况下无法使用。另一方面,文章 InvokeRestMethod for the Rest of Us 中描述的替代方案符合我们的需求:
$request = [System.Net.WebRequest]::Create($url)
$request.Method="Get"
$response = $request.GetResponse()
$requestStream = $response.GetResponseStream()
$readStream = New-Object System.IO.StreamReader $requestStream
$data=$readStream.ReadToEnd()
if($response.ContentType -match "application/xml") {
$results = [xml]$data
} elseif($response.ContentType -match "application/json") {
$results = $data | ConvertFrom-Json
} else {
try {
$results = [xml]$data
} catch {
$results = $data | ConvertFrom-Json
}
}
$results
但是它仅适用于GET方法。请问您能否建议如何扩展此代码示例以使用POST
方法发送请求正文(类似于Invoke-RestMethod
中的Body
参数)?
$Body = [reports[]]$filecontent;
我在这里有一个问题(http://stackoverflow.com/questions/39144921/how-to-pass-an-array-via-web-request),现在我看到了这个答案。你觉得这个解决方案在我的情况下会起作用吗? - gofr1