【问题标题】:Invoke-Restmethod: how do I get the return code?Invoke-Restmethod:我如何获得返回码?
【发布时间】:2022-04-25 14:05:11
【问题描述】:

在 PowerShell 中调用 Invoke-RestMethod 时,有没有办法将返回码存储在某处?

我的代码如下所示:

$url = "http://www.dictionaryapi.com/api/v1/references/collegiate/xml/Adventure?key=MyKeyGoesHere"

$XMLReturned = Invoke-RestMethod -Uri $url -Method Get;

我在 $XMLReturned 变量中的任何地方都没有看到返回码 200。我在哪里可以找到该返回码?

【问题讨论】:

    标签: api powershell


    【解决方案1】:

    您有几个选择。选项 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 }
    

    您可以尝试以下两种方法。

    【讨论】:

    • 第一种方法需要异常,所以它不适用于提问者获得 200 响应的场景。 Invoke-WebRequest 是要走的路;它不是“旧的”;它不需要try/catch 或例外。您可能需要稍微编辑答案以显示这一点,并解释 Invoke-RestMethod 只是将内容从 JSON 自动转换为对象,这可以通过将内容管道传输到 ConvertFrom-Json 使用 iwr 来实现。
    • 谢谢,很快就会这样做。我的意思是旧的,因为 invoke-restmethod 应该在某个时候替换它,但很好的呼吁也不需要 catch!
    • Invoke-RestMethod 和 Invoke-WebRequest 同时添加;两者都不是另一个的替代品(如果有的话iwr 取代irm,因为它更通用)。 irm 是您可以使用 iwr 和 ConvertFrom-Json 执行操作的快捷方式,只是让事情变得更快。如果你改进它,我会支持你的答案。
    • 目前尚不清楚这个到 ConvertFrom-Json 的管道是如何工作的。我把内容(看起来像一个字节流)输入到 ConvertFrom-Json 并得到一个(你猜对了)字节流。因此,如果对此进行了编辑,则需要进行更多说明。
    • @briantist Invoke-WebRequest 对于不成功的 HTTP 消息需要 try/catch 块。 docs.microsoft.com/en-us/powershell/module/…
    【解决方案2】:

    简短的回答是:你不能。
    你应该改用Invoke-WebRequest。

    两者非常相似,主要区别在于:

    • Invoke-RestMethod 仅返回 响应正文,方便地预先解析。
    • Invoke-WebRequest 返回完整响应,包括响应标头和状态代码,但不解析响应正文。
    PS> $response = Invoke-WebRequest -Uri $url -Method Get
    
    PS> $response.StatusCode
    200
    
    PS> $response.Content
    (…xml as string…)
    

    【讨论】:

    • 我正在使用PowerShell从Artifactory下载一个JAR文件,但是响应为空或者什么都没有,所以没有状态码。如何检查请求是否成功?
    【解决方案3】:

    Invoke-WebRequest 会解决您的问题

    $response = Invoke-WebRequest -Uri $url -Method Get
    if ($response.StatusCode -lt 300){
       Write-Host $response
    }
    

    如果需要,请尝试接听电话

    try {
       $response = Invoke-WebRequest -Uri $url -Method Get
       if ($response.StatusCode -lt 300){
          Write-Host $response
       }
       else {
          Write-Host $response.StatusCode
          Write-Host $response.StatusDescription
       }
    }
    catch {
       Write-Host $_.Exception.Response.StatusDescription
    }
    

    【讨论】:

      【解决方案4】:

      PowerShell 7 为 Invoke-RestMethod cmdlet 引入了参数 StatusCodeVariable。传递不带美元符号 ($) 的变量名:

      $XMLReturned = Invoke-RestMethod -Uri $url -Method Get -StatusCodeVariable 'statusCode'
      
      # Access status code via $statusCode variable
      

      【讨论】:

        【解决方案5】:

        我看到有人推荐 Invoke-WebRequest。你应该知道它是基于 IE 的。我收到以下错误

        无法解析响应内容,因为 Internet Explorer 引擎不可用,或者 Internet Explorer 的首次启动配置不完整。指定 UseBasicParsing 参数,然后重试。

        我知道我第一次可能会打开 IE,但编写脚本的整个想法是避免像 IE 这样的依赖项。 Invoke-RestMethod 没有这个问题。

        【讨论】:

        • 异常告诉你该怎么做 - 将-UseBasicParsing 传递给命令。在调用 REST 方法时,您无论如何都不需要 ParsedHtml 属性(更不用说这会导致 HTML 页面的 Javascript 代码运行)。见docs.microsoft.com/en-us/powershell/module/…。
        猜你喜欢
        • 2015-06-20
        • 1970-01-01
        • 2021-07-19
        • 2013-09-17
        • 1970-01-01
        • 1970-01-01
        • 2016-06-29
        • 2015-02-04
        • 1970-01-01
        相关资源
        最近更新 更多