【问题标题】:How to get HTTP status on Invoke-WebRequest/Invoke-RestMethod when using -OutFile parameter?使用 -OutFile 参数时如何在 Invoke-WebRequest/Invoke-RestMethod 上获取 HTTP 状态?
【发布时间】:2015-07-23 00:03:30
【问题描述】:

我有一个对 RESTful API 的请求(注意,Invoke-WebRequestInvoke-RestMethod 都会发生这种情况):

$resp1 = Invoke-WebRequest -OutFile $clusterConfigZipFile -Uri $apiFolder -Body $fileContent -Method 'POST' -Credential $adminCredentials -ContentType "application/x-www-form-urlencoded" -ErrorAction SilentlyContinue -Verbose

我知道它可以正常工作,因为:

  • 它会下载一个正确的 zip 文件。
  • 如果我使用 Fiddler,我可以看到响应标头 (HTTP 200)

我的问题是 $resp1 中的代码没有返回任何内容。我有read,当您使用-OutFile 时,它是一个“功能”。

有没有办法在 PowerShell 中捕获响应或至少响应代码?

【问题讨论】:

    标签: rest powershell powershell-3.0


    【解决方案1】:

    作为documented,如果您希望除了写入文件之外还希望返回响应,请使用-Passthru 参数。

    -OutFile

    将响应正文保存在指定的输出文件中。输入路径和文件名。如果省略路径,则默认为当前位置。

    默认情况下,Invoke-WebRequest 将结果返回到管道。要将结果发送到文件和管道,请使用 Passthru 参数。

    【讨论】:

      猜你喜欢
      • 2017-08-17
      • 2017-07-26
      • 1970-01-01
      • 2022-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多