方法 PUT IN Powershell,问题:400 Malformed Error Converting,

Method PUT IN Powershell, Issue: 400 Malformed Error Converting,

当运行 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 客户端 RESTer 上测试了参数。

您在 $body 定义中混用了 PowerShell 和 JSON 表示法。

改变这个:

$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"
        ]
    }
}
'@