Sam Williams
Sam Williams

Reputation: 63

Post to json rest API from powershell

Hi can someone tell me where i'm going wrong here, i get the following error executing an API call from powershell. I have tried to format the body i many ways but cannot get it to work.

Invoke-RestMEthod : Cannot send a content-body with this verb-type.
At line:7 char:20
+ ... sponseKan = Invoke-RestMEthod -Uri $kanboardserver  -ContentType "app ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], ProtocolViolationException
    + FullyQualifiedErrorId : System.Net.ProtocolViolationException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

My Code;

  $headersKan = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" -ErrorAction Stop
$headersKan.Add("X-API-Auth", "jsonrpc:$kanboardtoken")
$bodyKan = @{jsonrpc = "2.0"
            id = "1"
            method = "getAllProjects"
            } | ConvertTo-Json
$responseKan = Invoke-RestMEthod -Uri $kanboardserver  -ContentType "application/json"  -Headers $headersKan -Body $bodyKan -ErrorAction Stop
Write-Host $responseKan

Upvotes: 1

Views: 1406

Answers (1)

pfx
pfx

Reputation: 23214

The default http verb is GET which does not allow a body/payload. Pass POST as method argument and do not json-serialize the body yourself.

Upvotes: 3

Related Questions