Метод PUT IN Powershell, выпуск: 400 преобразований с ошибками, - PullRequest
0 голосов
/ 21 января 2019

При запуске сценария в Powershell, команда Invoke-RestMethod PUT выводит

> Invoke-RestMethod : 400 MalformedCONTENTThe data request is malformed. Required content is missing or empty.Could not acquire data. 

 + Invoke-RestMethod -Method PUT -Uri http://##.##.###.#:8022/reader/bl ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Изменил параметр (как сказано пользователем ниже) на:

$headers3 = @{
Host          = "##.##.###.#"
Authorization = "Basic Jlytuwhazkfnrfhdskldmxzaaldkjgpoiudtr"
Accept        = "application/json"
}   

$body = @{
       'payload' = @{
             'sensorId'   = "ee:16:as:ea:de:963"
             'blinkCount' = 5
             'blinkOn'    = 500
             'blinkOff'   = 500
             'countPause' = 2
             'timeout'    = 5000
                    }    
}

$jso = $body | ConvertTo-Json
Invoke-RestMethod -Method PUT -Uri http://##.##.###.#:8022/reader/blink-led -Headers $headers3 -Body $jso

Я изменилпараметр $ body несколько раз и все еще не мог получить вывод в Powerhsell.Параметры работают, так как я проверил параметр на Rest Client, RESTer.

1 Ответ

0 голосов
/ 23 января 2019

Вы смешиваете записи PowerShell и JSON в своем определении $body.

Изменить это:

$body = @{
    sourceName = "Anything here";
    sourceId = "Anything here ";
    sourceIP = "##.##.##.###";
    sourcePort = ####;
    datetime = "###############";
    payload = {
        monitors = [
            "REST response time",
            "Authentication failures"
        ]
    }
}

либо в это:

$body = @{
    'sourceName' = 'Anything here'
    'sourceId'   = 'Anything here '
    'sourceIP'   = '##.##.##.###'
    'sourcePort' = ####
    'datetime'   = '###############'
    'payload'    = @{
        'monitors' = 'REST response time',
                     'Authentication failures'
    }
}

или это:

$body = @'
{
    "sourceName": "Anything here",
    "sourceId": "Anything here ",
    "sourceIP": "##.##.##.###",
    "sourcePort": ####,
    "datetime": "###############",
    "payload": {
        "monitors": [
            "REST response time",
            "Authentication failures"
        ]
    }
}
'@
...