You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell调用JSON REST API报错,请求协助排查问题

解决PowerShell调用Kanboard API时的ProtocolViolationException错误

嘿,这个问题我之前也碰到过!错误提示Cannot send a content-body with this verb-type已经点明了核心问题——你当前使用的HTTP请求方法不允许携带请求体。

错误原因分析

PowerShell的Invoke-RestMethod默认使用GET请求,而HTTP规范里GET请求是不应该携带请求体的。但Kanboard的JSON-RPC API要求使用POST请求来传递JSON格式的调用参数,这就导致了冲突,抛出了ProtocolViolationException。另外你的代码里还有个小拼写错误:Invoke-RestMEthod应该是Invoke-RestMethod(最后两个字母E和T写反了),这个也可能影响执行。

修正后的代码

$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
# 1. 修正命令拼写错误;2. 添加-Method POST参数
$responseKan = Invoke-RestMethod -Uri $kanboardserver -ContentType "application/json" -Headers $headersKan -Body $bodyKan -Method POST -ErrorAction Stop
Write-Host $responseKan

关键改动说明

  • 添加-Method POST参数:明确告诉PowerShell使用POST请求,这样就可以合法地传递请求体了
  • 修正命令拼写:把Invoke-RestMEthod改成正确的Invoke-RestMethod
  • 规范哈希表格式:给哈希表的键加上引号,虽然PowerShell允许省略,但这样写可读性更强,也避免特殊字符带来的问题

这样修改后,应该就能成功调用Kanboard的getAllProjects API了。

内容的提问来源于stack exchange,提问作者Sam Williams

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:02:15