How to properly format PSObject to pass to PUT method for InvokeRest Method

265 views Asked by At

Below is the key value pairs I need to pass to the API. Dont know where Im going wrong

$Body = @{
    
    hostName            = 'null'
    hostOperatingSystem = 0
    optionalFilter1     = 'null'
    optionalFilter2     = 'null'
    description         = 'null'
    serviceName         = "ServiceManager.Logging"
    sectionName         = "Services"
    signature           = 'null'
    value               = @( @{
            Services = @( @{
                              
                    FullPath      = "..\Service\MicroService.exe"
                    InstanceCount = 2
                })
                            
                          
        })
} | ConvertTo-Json -Depth 4


  $Response = Invoke-RestMethod -Uri $URI -Method PUT -Headers $Headers -Body $Body -ContentType 'application/json'

Below is the error I am getting

Invoke-RestMethod : {"":["JsonToken EndArray is not valid for closing JsonType Object. Path '', line 14, position 15."],"value":["Unexpected character encountered while parsing value: [. Path 'value', line 5, position 15."]}
1

There are 1 answers

1
Naldo Ash On
$Body = @{
    
    hostName            = '';
    hostOperatingSystem = 0;
    optionalFilter1     = '';
    optionalFilter2     = '';
    description         = '';
    serviceName         = "ServiceManager.Logging";
    sectionName         = "Services";
    signature           = 'null';
    value               = '{"Services": [{"Fullpath":"..\\MicroService.exe","InstanceCount": 2}]}'

} | ConvertTo-Json -Depth 10

This was the correct format