Invoke-Restmethod: how do I get the return code?

前端 未结 2 1650
轻奢々
轻奢々 2021-02-05 01:30

Is there a way to store the return code somewhere when calling Invoke-RestMethod in PowerShell?

My code looks like this:

$url = \"http://www         


        
2条回答
  •  星月不相逢
    2021-02-05 01:59

    So the short answer is: You can't.
    You should use Invoke-WebRequest instead.

    The two are very similar, the main difference being:

    • Invoke-RestMethod returns the response body only, conveniently pre-parsed
    • Invoke-WebRequest returns the full response, including response headers and status code, but without parsing the response body.
    PS> $response = Invoke-WebRequest -Uri $url -Method Get
    
    PS> $response.StatusCode
    200
    
    PS> $response.Content
    (…xml as string…)
    

提交回复
热议问题