Post/ /v3/search, PowerShell 7, Invoke-RestMethod

I am exploring PowerShell 7 and Invoke-RestMethod. I am struggling to find a solution on setting the proper syntax for -Body

In Postman is can do a POST /v3/search?count=true&offset=0&limit=2000, with a Body of:
{
“query”: {
“query”: “*”
},
“indices”: [
“roles”
],
“sort”: [
“name”
],
“includeNested”: false
}

I am using the $Form variable in my script with the -Body parameter
Call: $responseObj = Invoke-RestMethod -Headers $postHeaders -Body $Form -Method ‘POST’ -Uri $uploadURL

I have tried more than the below 3 attempts to set $Form.

$From =‘{
“query”: {
“query”: “*”
},
“indices”: [
“roles”
],
“sort”: [
“name”
],
“includeNested”: false
}’

$From =‘{
“query”: {
“query”: “*”
},
“indices”: [
“roles”
],
“sort”: [
“name”
],
“includeNested”: false
}’ | convertfrom-json

$Form = @{
query = ‘“query”: “*”’
indices = ‘roles’
‘sort’ = ‘name’
“includeNested”=‘false’
}
}

I receive a “400.0 Bad request syntax”, “The request could not be parsed.” If I run the line without -body, I receive a “400.1.0 Required data missing or empty”, “Required field "search" was missing or empty.”

I am new to the whole development role and would appreciate any insight/assistance.

Regards.

Hi Tyler,

Will you please try with this call and using $JSON instead of $Form – I believe this should work:


$JSON = @'
{
  "indices": [
    "roles"
  ],
  "query": {
    "query": "*"
  },
  "sort": ["name"]
}
'@

$responseObj = Invoke-RestMethod -Uri $uploadURL -Headers $postHeaders -Body $JSON -Method Post -ContentType "application/json"

Thanks,
Lisa Ivy

Sorry, I missed that you wanted includeNested parameter in the query – this is the JSON:

$JSON = @'
{
  "indices": [
    "roles"
  ],
  "query": {
    "query": "*"
  },
  "sort": ["name"],
  "includeNested": true
}
'@

That was what I needed. Amazing response time and spot on direction. Thanks Lisa!