CURL Body convert to Powershell Core Invoke-Restmethod Body using []

Hi,
I'm new to this forum and a starting scripter and I'm trying to figure out what I'm doing wrong here and hope to get some help.
I need to sent an Powershell Core Invoke-RestMethod POST command.
I use this already successfully where the 'body' doesn't contain any square brackets, but with this POST the API requires them according to the developer (a user can have multiple entries for the access- and secretkey).
When I use the CURL POST command with the following Body the API works fine:
-d '[ { "ObjectStorageLocation":"Location", "ObjectStorageAccount":1234, "ObjectStorageGroup":"1234-01", "ObjectStorageUser":"1234-10", "Accesskey":"455fs2457jkio", "Secretkey":"sg56hniitf3466", "Active":1 } ]'
But when I'm trying to perform the same using Powershell Core Invoke-Restmethod the body isn't accepted:
$body = @[{

ObjectStorageLocation=“Netherlands Location 01”

ObjectStorageAccount=1234

ObjectStorageGroup=“1234-01”

ObjectStorageUser=“1234-10”

Accesskey=“455fs2457jkio”

Secretkey=“sg56hniitf3466”

Active=1

}]

$jsonbody = $body | ConvertTo-Json
$response = Invoke-RestMethod $url -Method Post -Body $jsonbody -Headers $headers
The error is as follows:
422: Unprocessable Entity The request was well-formed but was unable to be followed due to semantic errors.
Can anyone help me out here?
Thanks in advance!
 

The square brackets represent an array, but typically it’s not required for a single entry. Try something like so:

$body = @()

$body += [pscustomobject]@{
    ObjectStorageLocation= "Netherlands Location 01"
    ObjectStorageAccount = 1234
    ObjectStorageGroup   = "1234-01"
    ObjectStorageUser    = "1234-10"
    Accesskey            = "455fs2457jkio"
    Secretkey            = "sg56hniitf3466"
    Active               = 1   
}

$body += [pscustomobject]@{
    ObjectStorageLocation= "Netherlands Location 01"
    ObjectStorageAccount = 1234
    ObjectStorageGroup   = "1234-01"
    ObjectStorageUser    = "1234-10"
    Accesskey            = "455fs2457jkio"
    Secretkey            = "sg56hniitf3466"
    Active               = 1   
}

$body | ConvertTo-Json

Hi Rob,

 

thanks for the reply.

I will test it later this week and let you know the outcome.

Regards,

Matty

Hi Rob,

 

just tested it and still having an issue.

The body now looks as follows:

[
{
"ObjectStorageLocation": "Netherlands Location 01",
"ObjectStorageAccount": 9917,
"ObjectStorageGroup": "grp-9917-01",
"ObjectStorageUser": "usr-9917-10",
"Accesskey": "40c9e34bc7071e178a11",
"Secretkey": "3guFFjD/XZK+ApubpRw4WeVRwzR0Smwl53AAY8Uu",
"Active": 1
},
{
"ObjectStorageLocation": "Netherlands Location 01",
"ObjectStorageAccount": 9917,
"ObjectStorageGroup": "grp-9917-01",
"ObjectStorageUser": "usr-9917-10",
"Accesskey": "40c9e34bc7071e178a11",
"Secretkey": "3guFFjD/XZK+ApubpRw4WeVRwzR0Smwl53AAY8Uu",
"Active": 1
}
]

But needs to be just ‘single’:

[
{
"ObjectStorageLocation": "Netherlands Location 01",
"ObjectStorageAccount": 9917,
"ObjectStorageGroup": "grp-9917-01",
"ObjectStorageUser": "usr-9917-10",
"Accesskey": "40c9e34bc7071e178a11",
"Secretkey": "3guFFjD/XZK+ApubpRw4WeVRwzR0Smwl53AAY8Uu",
"Active": 1
}
]

 

Any ideas?

Your help is really appreciated!

 

Hi. Trying using the here-string as the body of you Inovoke-RestMethod. There is no need to convert the JSON into PowerShell and then convert it back to JSON.

[pre]
$here = @"
[
{
“ObjectStorageLocation”: “Netherlands Location 01”,
“ObjectStorageAccount”: 9917,
“ObjectStorageGroup”: “grp-9917-01”,
“ObjectStorageUser”: “usr-9917-10”,
“Accesskey”: “40c9e34bc7071e178a11”,
“Secretkey”: “3guFFjD/XZK+ApubpRw4WeVRwzR0Smwl53AAY8Uu”,
“Active”: 1
}
]
"@
[/pre]

pwshliquori

Always I forgot to mention, since you are using PowerShell Core, you can take advantage of the -AsArray parameter when converting to JSON.

$JsonBody = $Body |ConvertTo-Json -AsArray

pwshliquori

Hi pwshliquori,

tested and seems to work perfectly!

Thanks for your help!

 

Regards,

 

Matty