I have the following logic that sets values on a json object ... then tries to convert to a json string, stuff into the Body of an HTTP request. When I attempt the POST the request fails because the body is empty. But the debug print that outputs the $widget just before I attempt the assignment to Body looks valid.
Code Looks like this:
$widget = (Get-Content './testdata/notification.json') | ConvertFrom-Json
Write-Output $widget.GetType();
Write-Output $sourcedata.GetType();
$widget.requestId = $sourcedata.requestId
$widget.status = "provisioned"
$widget.workspace.id = $sourcedata.workspace.id
$widget.workspace.isReadOnly = "false"
$widget.workspace.isOnDedicatedCapacity = $sourcedata.workspace.isOnDedicatedCapacity
$widget.workspace.name = $sourcedata.workspace.name
$widget.workspace.owners = $sourcedata.workspace.owners
# Convert to Json
$widget | ConvertTo-Json -Depth 5
Write-Output $widget
$params = @{
Uri = "http://localhost:7071/workspace/notification"
Method = "POST"
Headers = @{
'Authorization' = 'Bearer ' $token
'Content-Type' = 'application/json'
}
'Body'= $widget
}
$notificationResult = Invoke-RestMethod @params
Any tips would be appreciated.
What's interesting is that when I try to print the object type of the widgets object just after I assign it all the values and convert to json, nothing comes back.
Write-Output $widget.GetType()
returns:
True False PSCustomObject System.Object
instead of something like:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
CodePudding user response:
Even though I don't have too much context on the particulars about your REST api, I think I can spot the problems, as I've experienced a number of variations of it myself.
Problem #1: ConvertTo-JSON is unrolling objects
# When you pipe to ConvertTo-JSON, you're actually saying:
# Give me a JSON object for each item in the list.
$widget | ConvertTo-Json -Depth 5
Solution:
# Assuming there was more than one widget, this will produce a JSON array
ConvertTo-JSON -Depth 5 -InputObject $widget
# If you need to force it into a JSON array, you can always use the array subexpression:
ConvertTo-JSON -Depth 5 -InputObject @($widget)
Problem #2: Invoke-RestMethod -Body does not auto-convert JSON.
When you call Invoke-RestMethod with -Body $widget $widget is not JSON. Invoke-RestMethod will not magically make your body JSON, since lots of REST apis take formats other than JSON.
Solution: Assign the converted value and pass that instead.
# Convert to Json
$widgetJson = ConvertTo-Json -Depth 5 -InputObject $widget
Write-Output $widget
$params = @{
Uri = "http://localhost:7071/workspace/notification"
Method = "POST"
Headers = @{
'Authorization' = 'Bearer ' $token
'Content-Type' = 'application/json'
}
'Body'= $widgetJson
}
I don't know if there would be additional problems in your object's structure that would make the REST api complain, but these are two significant issues in your script that I was able to identify from the symptoms you've presented.
Hope this Helps