简体   繁体   中英

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??

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}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM