Powershell Invoke-Webrequest w/ JSON Body - Can not deserialize...?
Use Invoke-RestMethod
instead of Invoke-WebRequest
.
If you have the body as a string use:
Invoke-RestMethod -Uri http://your-url.com -Method POST -Body $body_json -ContentType "application/json"
If the body must be constructed from data/parameters, it might be easier to build a hashtable and convert it to json via ConvertTo-Json
:
$body_json = @{
datasource = @(
@{
parentId = 123456789000
name = "name"
id = "value"
typeId = 0
childEnabled = $false
childCount = 0
childType = 0
ipAddress = "ipAddress"
zoneId = 0
url = "url"
enabled = $false
idmId = 123456789000
parameters = @( @{
key = "key"
value = "value"
})
})} | ConvertTo-Json -Depth 4
Invoke-RestMethod -Method 'Post' -Uri http://your-url.com -Body $body_json -ContentType "application/json"
Related videos on Youtube
J. Doe
Updated on June 04, 2022Comments
-
J. Doe almost 2 years
I need to perform an Invoke-Webrequest with a specifically formatted body to add devices to a product. Here is what it looks like in json (example straight from the vendor's documentation):
$body_json = '{"datasource": [{ "parentId": "123456789000", "name": "(name)", "id": "(value)", "typeId": 0, "childEnabled": false, "childCount": 0, "childType": 0, "ipAddress": "(ipAddress)", "zoneId": 0, "url": "(url)", "enabled": false, "idmId": 123456789000, "parameters": [{ "key": "(key)", "value": "(value)" }] }]}'
When I try to submit this in its json representation though, I get the following error:
Invoke-WebRequest : Can not deserialize instance of com.vendor.etc.DataSourceDetail out of START_ARRAY token at [Source: java.io.StringReader@22c614; line: 1, column: 1] At C:\powershell_script_location\ps.ps1:114 char 9 + $request = Invoke-WebRequest $url -Method Post -Headers $headers -Body $body_json - ... +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
The issue is with the format of the "parameters", parameter because the request submits fine when omitting the "parameters", but then the devices that I'm adding are missing important parameter details.
Is there something wrong with
Invoke-WebRequest, JavaScriptSerializer,
the vendor's code, or is this a user error? Let me know if any clarification is needed.Unfortunately I don't know what a
com.vendor.etc.DataSourceDetail
instance looks like, as I am using an API and I don't have access to it directly. -
J. Doe almost 6 yearsEnded up figuring this out after I posted. The hashtable solution works great but I also had to add some -Depth onto the ConvertTo-Json otherwise it wouldn't get all of the key/values in 'parameters' without it. Thanks!
-
mklement0 almost 6 years@J.Doe: pfx's method of constructing the JSON is definitely preferable, but the resulting JSON is equivalent to the string-literal one in your question. So I gather it was just the switch from
Invoke-WebRequest
toInvoke-RestMethod
that made the difference?