【问题标题】:Invoke-WebRequest : Cannot bind parameter 'Headers'Invoke-WebRequest:无法绑定参数“标头”
【发布时间】:2017-12-24 07:11:58
【问题描述】:

我正在尝试在 powershell 中执行 curl 命令:

curl --user bitcoinipvision --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "move", "params": ["acc-1", "acc-2", 6, 5, "happy birthday!"] }' -H 'content-type: application/json;' http://localhost:18332/

但是我得到这个错误,是什么问题?

Invoke-WebRequest:无法绑定参数“标头”。无法转换 “内容类型:应用程序/json;”要键入的“System.String”类型的值 “System.Collections.IDictionary”。 在行:1 字符:158 + ... 5, "生日快乐!"] }' -H 'content-type: application/json;' http:// ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException + FullyQualifiedErrorId:CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

【问题讨论】:

  • 试试alias curl,它显然是Invoke-WebRequest(虽然不在我的PowerShell上)。如果你想要真正的curl,请明确使用curl.exe。
  • 您将内容提到为 application/json 而您将其作为字符串传递。如果它的正确 json 则使用 convertto-json 将其转换为 json
  • 请注意,最新版本的 PowerShell 具有 Invoke-RestMethod 并完全支持 JSON(ConvertTo-Json 等)。值得阅读有关执行此操作的原生 PowerShell 方法。
  • 这些Invoke-RestMethod examples 会有所帮助,因为我觉得文档不太好。

标签: powershell curl


【解决方案1】:

正如一些评论者已经指出的,您会看到curl 实际上只是Invoke-WebRequest 的别名:

PS> Get-Command curl

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           curl -> Invoke-WebRequest

注意:我建议使用Get-Command,而不是Get-Alias,因为您可能不知道您使用的命令是别名、cmdlet 还是可执行文件。

从这一点来看,有两种方法可以解决您的问题:

  1. 使用 PowerShell 的 Invoke-RestMethod(或者,如果您使用 PowerShell Invoke-WebRequest):

    Invoke-RestMethod -Uri http://localhost:18332/ -Credential bitcoinipvision -body $thisCanBeAPowerShellObject 
    

    如您所见,不需要内容类型,因为 JSON 是 IRM 的默认内容类型;虽然您可以使用-ContentType 更改它。

  2. 如果在您当前的环境中可用,请使用原始的cUrl。你必须这样输入:

    curl.exe --user bitcoinipvision --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "move", "params": ["acc-1", "acc-2", 6, 5, "happy birthday!"] }' -H 'content-type: application/json;' http://localhost:18332/
    

我肯定更喜欢第一个而不是第二个,因为 PowerShell 本身支持 JSON 答案,这使您可以轻松使用它们,例如通过管道将其发送到Where-Object、Format-Table、Select-Object、Measure-Object 等等。如果您更喜欢使用 cUrl,则必须自己解析 curl.exe 进程返回的字符串。这也可能是二进制内容的问题。

【讨论】:

    【解决方案2】:

    Curl 基本上使用 PowerShell 中的 Invoke-Webrequest。

    正如您在错误中看到的那样,标头基本上接受“System.Collections.IDictionary”n 形式,并且您正在传递“System.String”。

    将标头转换为字典/哈希表可以解决问题,

    curl --user bitcoinipvision --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "move", "params": ["acc-1", "acc-2", 6, 5, "happy birthday!"] }' -H @{ "content-type" = "application/json"} http://localhost:18332/
    

    【讨论】:

      【解决方案3】:

      我有一个类似的问题,直到我发现这个: https://github.com/MicrosoftDocs/azure-docs/issues/31851

      “这些命令旨在在特定 VM 和 Bash 环境中运行。不在 PowerShell 中。因此,在大多数情况下,您看到的错误都是意料之中的。”

      【讨论】:

        猜你喜欢
        • 2017-11-30
        • 2017-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-21
        • 2021-07-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多