WebRequest : Cannot bind parameter 'Headers'

I am trying to execute a curl command in powershell:

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

But I get this error, what is the problem?

Invoke-WebRequest : Cannot bind parameter 'Headers'. Cannot convert the "content-type: application/json;" value of type "System.String" to type "System.Collections.IDictionary". At line:1 char:158 + ... 5, "happy birthday!"] }' -H 'content-type: application/json;' http:// ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand


As already stated out by some commenters you will see that curl is actually just an alias to Invoke-WebRequest :

PS> Get-Command curl

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

Note: I suggest to use Get-Command , not Get-Alias because you maybe don't know if the command you are using is an alias, cmdlet, or an executable.

From this point there are two possible ways to solve your issue:

  • Use PowerShell's Invoke-RestMethod (or, if you are using PowerShell < 3, Invoke-WebRequest ):

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

    As you can see, no content-type is needed as JSON is IRM's default content Type; though you can change it using -ContentType .

  • If available in your current environment, use the original cUrl . You have to type it this way:

    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/
    
  • I would definitely prefer the first one over the 2nd one, as PowerShell natively supports JSON answers, which allows you to easily use them, eg by piping it to Where-Object , Format-Table , Select-Object , Measure-Object and so much mure. If you prefer to use cUrl, you have to parse the String returned by the curl.exe process on your own. This could also be problematic with binary content.

    链接地址: http://www.djcxy.com/p/37884.html

    上一篇: 对象和HTML输出问题

    下一篇: WebRequest:无法绑定参数'标题'