59

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 birthday!"] }' -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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Zenith
  • 1,037
  • 1
  • 10
  • 21
  • 31
    Try `alias curl`, it apparently goes to `Invoke-WebRequest` (not on my PowerShell though). If you want the real `curl`, explicitly use `curl.exe`. – Jeroen Mostert Jul 19 '17 at 07:30
  • 1
    you have mentioned the content as application/json whereas you are passing it as string. If its proper json then use convertto-json to get it converted to json – Ranadip Dutta Jul 19 '17 at 07:56
  • Note that recent versions of PowerShell have [`Invoke-RestMethod`](https://msdn.microsoft.com/powershell/reference/5.1/microsoft.powershell.utility/invoke-restmethod) and full support for JSON (`ConvertTo-Json` and the like). It's worth reading up on native PowerShell ways to do this. – Jeroen Mostert Jul 19 '17 at 07:57
  • 1
    These [Invoke-RestMethod examples](https://www.jokecamp.com/blog/invoke-restmethod-powershell-examples/) will help as I didn't find the documentation great. – henrycarteruk Jul 19 '17 at 10:20

3 Answers3

54

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:

  1. 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.

  2. 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, e.g. 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.

Clijsters
  • 4,031
  • 1
  • 27
  • 37
13

Curl basically uses Invoke-Webrequest in PowerShell.

As you can see in the error, the header basically accepts the form "System.Collections.IDictionary"n and you are passing through a "System.String".

Converting the Header to a dictionary/hashtable would resolve the issue,

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/
Akash Masand
  • 1,441
  • 14
  • 30
0

I had a similar issue, until i found this: https://github.com/MicrosoftDocs/azure-docs/issues/31851

"The commands are designed to be ran within the specific VM and in a Bash environment. Not in PowerShell. So the error you see would be expected in most cases."