1

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
Sam Williams
  • 63
  • 2
  • 7
  • You are not showing all the pertinent code in your post. You have variables that are not defined and not used in your post as well as variable not defined, and used in your post. For example: "jsonrpc:$kanboardtoken" and $kanboardserver. – postanote May 26 '18 at 23:59

1 Answers1

3

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.

pfx
  • 20,323
  • 43
  • 37
  • 57
  • yes - start with `-Method Post` tagged on to the `Invoke-RestMethod` cmdlet and go from there... – Adam Parsons May 27 '18 at 00:04
  • See [here](https://stackoverflow.com/questions/35722865/how-to-make-a-post-request-using-powershell-if-body-have-a-parameter-type) for an example – sodawillow May 27 '18 at 12:01