Invoke-WebRequest: Unable to bind 'Headers' parameter

I am trying to execute 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: Unable to bind the Headers parameter. Unable to convert "content-type: application / json;" value of type "System.String" for enter "System.Collections.IDictionary". On the line: 1 char: 158 + ... 5, "Happy Birthday!" ]} '-H' content-type: application / json; 'http: // ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo: InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException + FullyQualifiedErrorId: CannotConvertArgumentNoMessage, Microsoft.PowerShell.Commands.InvokeWebRequestCommand

+5
source share
1 answer

As already mentioned by some commentators, you will see that curl is actually just an alias for Invoke-WebRequest :

 PS> Get-Command curl CommandType Name Version Source ----------- ---- ------- ------ Alias curl -> Invoke-WebRequest 

Note. I suggest using Get-Command rather than Get-Alias because you may not know if the command you are using is an alias, cmdlet or executable.

From now on, there are two possible ways to solve your problem:

  • Use PowerShell Invoke-RestMethod (or, if you use PowerShell <3, Invoke-WebRequest ):

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

    As you can see, the content type is not required since JSON is the default IRM Type content; although you can change it using -ContentType .

  • If available in your current environment, use the original curl . You should enter it like this:

     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 prefer the first option in the second, since PowerShell natively supports JSON responses, which allows you to easily use them, for example. by feeding it to Where-Object , Format-Table , Select-Object , Measure-Object and so much mure. If you prefer to use cUrl, you need to parse the string returned by curl.exe its own. It can also be problematic with binary content.

0
source

Source: https://habr.com/ru/post/1269964/


All Articles