Author Posts

July 20, 2018 at 1:26 pm

Hello,
I have a query that works with Postman
Postman body
{
"ObjectDN": "toto",
"AttributeName": "Disabled",
"Values": ["1"]
}

I try in powershell
[code]
Powershell extract
$fullID = "toto"
function retire($fullID){
$json = @{ObjectDN=$fullID;AttributeName="Disabled";Values="1"}|ConvertTo-Json
$uri = $baseurl + '/Config/Write?apikey=' + $global:key
$statut = Invoke-RestMethod -Uri $uri -Body $json -Method POST -ContentType 'application/json'
if($statut.Result -eq $null){
return "error"
}else{
return $statut.Result
}
[/code]
When I run it I have an error
« Invoke-RestMethod : {"Error":"Unable to parse input document."} »

I think the problem is the conversion of the line
"Values": ["1"]
that works in Postman
but in powershell we can not use []
I'm looking for a solution
Bests regards

July 20, 2018 at 1:58 pm

Try generating your JSON like so:

$body = New-Object -TypeName PSObject -Property @{
    "ObjectDN" = "toto"
    "AttributeName" =  "Disabled"
    "Values" = @("1")
}

$jsonBody = $body | ConvertTo-Json

$jsonBody

Output:

{
    "AttributeName":  "Disabled",
    "Values":  [
                   "1"
               ],
    "ObjectDN":  "toto"
}

July 21, 2018 at 6:18 am

To point out a personal experience with one specific API that didn't seem to like any carriage returns in the JSON body, I had to also use the -compress parameter of ConvertTo-JSON and also wrap the value with single quotes.

$JSON=@{Name=Value} | ConvertTo-JSON -compress
$Body='$($JSON)'
Invoke-RESTMethod -body $Body #missing URI/Header/Method of course

July 23, 2018 at 3:00 pm

Hello
Thank you for yours answer I would test on my return from holidays
Yann