Question to "/v4/nodes/rooms/{room_id}/groups" API Call
Hello
I'm currently working on a small powershell script. I'm using the "/v4/nodes/rooms/{room_id}/groups" API Call. If I save the output into a variable the variable has the type "Microsoft.PowerShell.Commands.HtmlWebResponseObject".
After I get the content with $output = $output.content.
Now my output is the type "System.String". If I now look at the variable it contains a range and the actual items. Sadly since its a string I can't use loops to work with the items.
For example the API Call /v4/node/ gives me "System.Management.Automation.PSCustomObject" as a variabletype. After $output = $output.items the type is "System.Object[]". I can use loops now to work with the items.
Is there a way to convert the "System.String" into the "System.Object[]" so I can use loops ?
Best regards,
Benedikt Hild
-
Offizieller Kommentar
Hi Benedikt,
Here's an example how you can parse the response and loop through the items:
$Token = ... # Contains your OAuth access token
# Fetch group information
$Response = Invoke-WebRequest -URI "https://dracoon.team/api/v4/nodes/rooms/12345/groups" -Method Get -ContentType "application/json" -Headers @{Authorization=("Bearer {0}" -f $Token)}
$Content = ConvertFrom-Json $Response.content
for ($i=0; $i -lt $Content.items.length; $i++){
Write-Output $Content.items[$i]
}Please note that if your response contains more than 500 items, you need to use the offset parameter and the range model.
Best regards,
Michael
Aktionen für Kommentare -
Hi Benedikt,
the response of API calls is usually of type application/json. If you read the response content as System.String, you could use the ConvertFrom-Json cmdlet (link to docs) to parse the string to a custom object or hash table. As stated in the PowerShell docs, you may also want to use the Invoke-RestMethod cmdlet instead:
You can also use the
Invoke-RestMethod
cmdlet, which automatically converts JSON content to objects.The object schema of your request's response is very well documented in Swagger. To access this documentation, just add "/api/swagger-ui.html#/" to your DRACOON domain name (e.g. https://mydracoon123.dracoon.team/api/swagger-ui.html#/).
Hope that helps,
Sebastian
Bitte melden Sie sich an, um einen Kommentar zu hinterlassen.
Kommentare
3 Kommentare