1

When running the script in Powershell, the Invoke-RestMethod PUT command outputs

> 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

Changed the parameter (as told by user below) to:

$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

I have changed the $body parameter multiple times and still couldn't get the output in Powerhsell. The parameters works since I have tested the parameter on Rest Client, RESTer.

Sam Shurp
  • 57
  • 1
  • 5
  • Try using the ContentType parameter with a value of application/json with your Invoke-RestMethod (a lot of REST API's which accept JSON expects the ContentType header field with a value of application/json set explicitly) – bluuf Jan 21 '19 at 01:55
  • Your body definition (more specifically the `payload` key) is invalid. It uses JSON syntax instead of PowerShell syntax. Either fix that or define `$body` as a JSON string. `Invoke-RestMethod` should work with either input type. – Ansgar Wiechers Jan 21 '19 at 09:03
  • So, basically the GET request was failing due to a missing field in the request data. How do you expect anyone to troubleshoot that? As for the PUT request: what kind of API is that anyway? The string is neither JSON nor PowerShell. Also, the updated code cannot possibly work due to several syntax errors. Please create a [mcve] that demonstrates your problem, update your question with *that* code and the full error output from that code. You may also want to read [this](https://stackoverflow.com/a/38421525/1630171). – Ansgar Wiechers Jan 29 '19 at 00:23

1 Answers1

1

You're mixing PowerShell and JSON notation in your $body definition.

Change this:

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

into either this:

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

or this:

$body = @'
{
    "sourceName": "Anything here",
    "sourceId": "Anything here ",
    "sourceIP": "##.##.##.###",
    "sourcePort": ####,
    "datetime": "###############",
    "payload": {
        "monitors": [
            "REST response time",
            "Authentication failures"
        ]
    }
}
'@
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • @SamShurp Please edit your question if you need to provide additional information. Code in comments is unreadable. – Ansgar Wiechers Jan 25 '19 at 09:53
  • I have edited the $body and added in Content Type 'applicaton/json' and still cannot seem to get the output. I am sure that the parameter work since I have tested the parameter, $body, on the Rest Client. – Sam Shurp Feb 11 '19 at 01:10
  • Thank you Ansgar, I found the error. There were two errors; On the of error was in the body section and you were able to help me out. The other error was in the header section; I needed to put double quotation mark. Example: "Host", "Authorization" Thank you again. – Sam Shurp Feb 13 '19 at 00:52