Difficulty using Invoke-Restmethod in Powershell

Asked

Viewed 45 times

0

I am trying to create a script in Powershell, and in this script I need to take data from an application in webservice, and when doing the same, just returns as an arrayListObject, instead of the objects themselves.

The script is the following:

$dados = Invoke-RestMethod -Uri 'http://IPADDRESS/DADOS' -Method 'Get'

I’ve tried too:

$dados = Invoke-RestMethod -Uri 'http://IPADDRESS/DADOS' | ConvertTo-Json
$dados = Invoke-RestMethod -Uri 'http://IPADDRESS/DADOS' -Method 'Get' -ContentType "application/json"

What it returns is all the objects in the $data variable, in an arrayListObject, and I can’t manipulate the objects within that array. I can’t select $data.data, for example, just $data.arrayListObject.

  • It would be good to inform the address where you are getting the data to also do tests since the documentation of cmdlet Invoke-Restmethod informs that cmdlet output depends on the format of the recovered content and if the request returns JSON strings, Invoke-RestMethod returns a PSObject representing the strings. Have you tried ConvertTo-Json

  • The address is of an intranet that we use, so I thought it was better not to share, since it would not be possible to access. Regarding Convertto-Json, yes I tried to use, but without success.

  • make a Curl at the same address and see if it returns JSON. Example: curl http://IPADDRESS/DADOS -H "Accept: application/json"

  • I was unsuccessful using Curl, but after some searches, I was able to get the page header from Invoke-Webrequest, where it says: [Content-Type, application/json;charset=UTF-8]

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.