有没有办法在调用 Invoke-RestMethod
时将返回码存储在某处?在 PowerShell 中?
我的代码如下所示:
$url = "http://www.dictionaryapi.com/api/v1/references/collegiate/xml/Adventure?key=MyKeyGoesHere"
$XMLReturned = Invoke-RestMethod -Uri $url -Method Get;
我在
$XMLReturned
中没有看到任何地方变量返回码为 200。我在哪里可以找到该返回码?
最佳答案
你有几个选择。找到选项 1 here .它从异常中找到的结果中提取响应代码。
try {
Invoke-RestMethod ... your parameters here ...
} catch {
# Dig into the exception to get the Response details.
# Note that value__ is not a typo.
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
}
另一种选择是使用旧的 invoke-webrequest cmdlet 找到 here .
从那里复制的代码是:
$resp = try { Invoke-WebRequest ... } catch { $_.Exception.Response }
这些是您可以尝试的两种方法。
关于api - 调用-Restmethod : how do I get the return code?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38622526/