jsonpowershellansibleansible-towerinvoke-restmethod

Json Array in Powershell from Podto Powershell


I have the following Json in the body of a Postman. what would be equivalent code in Powershell to send rest API via invoke-webrequest or via invoke-restmethod?

Postman Body:

{
    "extra_vars": {
        "servername": "apicall1234",
        "servers": "server01,server02,server06,server-12345"
    }
}

Solution

  • You can pass a request body via the -Body parameter on either of the web cmdlets. As with Postman (or any other HTTP client), you'll need to provide an address and a method/verb.

    $uri = 'https://<url goes here>/path'
    $reqBody = '{ "extra_vars": { "servername": "apicall1234", "servers": "server01,server02,server06,server-12345" } }'
    
    Invoke-WebRequest -Uri $uri -Body $reqBody -Method Post