api - 调用-Restmethod : how do I get the return code?

标签 api powershell

有没有办法在调用 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/

相关文章:

swift - 响应请求的分页网址的最佳做法是什么

PowerShell System.Object[] 数组导出-CSV

powershell - 如何使用 powershell 在 docx 文件中搜索单词?

windows - 在任务计划程序中运行 .ps1 脚本时如何隐藏 PowerShell 窗口?

javascript - 如何使用 AJAX 访问 Recurly API?

actionscript-3 - 安全性:AS3 中的成就和分数 API

api - Docebo API 集成

ruby - 对 Pocket API 的此 OAuth 请求有什么问题?

powershell - powershell:使用参数启动程序的脚本?

regex - 仅使用起始值匹配多行事件