wolficool
wolficool

Reputation: 1

Powershell Invoke-Webrequest Body Rest-API

I wanted to add a Body in a Invoke-Webrequest

I am a newbie in Powershell Rest API Request...

So here is the example what i need to create it in a Body into a Powershell Scipt:

Body:
{
   "select":[
      "SERVICE.ID"
   ],
   "parameter":[
      {
         "field":"SERVICE.NAME",
         "value":HOSTNAME 
      }
   ]
}

How can i convert these into a Powershell Body ??

Upvotes: 0

Views: 1509

Answers (1)

Avshalom
Avshalom

Reputation: 8889

You can use a JSON Body directly with Invoke-Webrequest

$Body = @'
{
   "select":[
      "SERVICE.ID"
   ],
   "parameter":[
      {
         "field":"SERVICE.NAME",
         "value": "HOSTNAME"
      }
   ]
}
'@

Invoke-WebRequest -Uri [...] -Method Post -Body $Body

And you can convert it to PS Object using ConvertFrom-Json like this:

$Obj = $Body | ConvertFrom-Json

$Obj

select       parameter                              
------       ---------                              
{SERVICE.ID} {@{field=SERVICE.NAME; value=HOSTNAME}}

Upvotes: 1

Related Questions