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

ApiPowershell

Api Problem Overview


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

My code looks like this:

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

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

I don't see anywhere in my $XMLReturned variable a return code of 200. Where can I find that return code?

Api Solutions


Solution 1 - Api

You have a few options. Option 1 is found here. It pulls the response code from the results found in the exception.

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
}

Another option is to use the old invoke-webrequest cmdlet found here.

Code copied from there is:

$resp = try { Invoke-WebRequest ... } catch { $_.Exception.Response }

Those are 2 ways of doing it which you can try.

Solution 2 - Api

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…)

Solution 3 - Api

Invoke-WebRequest would solve your problem

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

Try catch the call if you want

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
}

Solution 4 - Api

PowerShell 7 introduced parameter StatusCodeVariable for the Invoke-RestMethod cmdlet. Pass a variable name without the dollar sign ($):

$XMLReturned = Invoke-RestMethod -Uri $url -Method Get -StatusCodeVariable 'statusCode'

# Access status code via $statusCode variable

Solution 5 - Api

I saw some people recommend Invoke-WebRequest. You should know that somehow it's based on IE. I get the following error

> The response content cannot be parsed because the Internet Explorer engine is not available, or Internet Explorer's first-launch configuration is not complete. Specify the UseBasicParsing parameter and try again.

I know I probably could open IE the first time but the whole idea of writing a script is to avoid dependencies like IE. Invoke-RestMethod does not have this issue.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser952342View Question on Stackoverflow
Solution 1 - ApiAli RazeghiView Answer on Stackoverflow
Solution 2 - ApiGrilseView Answer on Stackoverflow
Solution 3 - ApiTrungView Answer on Stackoverflow
Solution 4 - ApiCodeFullerView Answer on Stackoverflow
Solution 5 - ApiBaruch FishmanView Answer on Stackoverflow