Parsing results after calling PowerShell Invoke-RestMethod
Hi all,

Hoping someone can help as I am stuck on this for a couple of days now -

I am calling a REST api for a vendor's service using the Invoke-RestMethod
cmdlet. I was wondering :

- How can I retrieve the http status code (200, etc).
- if there was a way to get a COUNT of the elements returned if the query
is successful.

Here's a snippet of what I am trying -

$headers = @{ #Ask to send XML back so that it can be searched etc using
XPATH. Dont know how to do this using JSON.
Accept  = "application/xml"
}
Try
{
#build a URL like:
https://OURINSTANCe.service-now.com/api/now/table/sys_user?sysparm_query=email=ravi@gmail.com
<https://ourinstance.service-now.com/api/now/table/sys_user?sysparm_query=email=ravi@gmail.com> #to
see if this user is present in the user table
$Result  = Invoke-RestMethod -Credential $SNowCreds -Headers $headers
-Method Get -Uri ("https://" + $SNowInstance + ".
service-now.com/api/now/table/" + $Table + "sysparm_query=" + $AttribName +
"=" + $AttribValue)
return $Result.response.result.sys_id
#***How do we get the response code to make sure that the http call went
thro OK? If it is not 200, how can I retrieve the message that is returned
saying "constraint violation" etc..



#***If the http call is successful, check if we have exactly one record
retrieved - how do I do this?



}
catch
{
$Result = $_.Exception.Response.GetResponseStream()
        $Reader = New-Object System.IO.StreamReader($result)
        $ResponseBody = $Reader.ReadToEnd();
$message = "Unable to run a query on table" + $Table + "`tattribute name: "
+ $AttribName + "`tattribute value: " + $AttribValue + "`t" +
$_.Exception.Message + "`t" + $ResponseBody
writelog -msg ("ERROR `t" + $message)
return ""
}


thanks,
-Ravi
February 23rd, 2015 11:56am

Invoke-WebRequest returns XML and not HTML.   If the site or method do not exist it throws an exception.

Free Windows Admin Tool Kit Click here and download it now
February 23rd, 2015 12:58pm

Here is a little tutorial.

http://blogs.technet.com/b/heyscriptingguy/archive/2013/10/21/invokerestmethod-for-the-rest-of-us.aspx

February 23rd, 2015 12:59pm

That is correct, it will return either XML (if supported by the server) or json as specified in the "accept" header. However, it also returns HTTP status codes (200 etc) that can be seen using the advanced restful client from http://restforchrome.blogspot.com/ . however, I wanted to know if I could retrieve that status code:

Free Windows Admin Tool Kit Click here and download it now
February 23rd, 2015 1:39pm

How would you expect that would be possible?  The CmdLet retrieves the stream and decodes it.  If there is an error it throws an exception.  Are you getting an exception?

The outcome of incorrect arguments is completely up to the designer of the service.  It does not usually produce a page error.

February 23rd, 2015 1:44pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics