Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL command works in git bash but not in cmd and powershell

The folowing command works in git bash but not in cmd and powershell

curl -X POST http://localhost:5678/api/findgen -H 'Content-Type: application/json' -d '{"a": "Val 1","b": "Val 2","c": "Val 3","d": "Val 4"}' -o "file.json"

I get error in cmd such as -

curl: (6) Could not resolve host: application

curl: (6) Could not resolve host: Val 1,b

curl: (6) Could not resolve host: Val 2,c

curl: (6) Could not resolve host: Val 3,d

curl: (3) [globbing] unmatched close brace/bracket in column 6

What can be the issue?

like image 383
Raghav Gupta Avatar asked Nov 29 '19 08:11

Raghav Gupta


People also ask

Does curl command work in PowerShell?

curl in PowerShell uses Invoke-WebRequest . From PowerShell 3.0 and above, you can use Invoke-WebRequest , which is equivalent to curl .

Why is curl command not working?

We might have come across errors like “curl: command not found” while working in the terminal. This type of error comes due to only one reason: the relevant package is not installed. Curl is a very popular data transfer command-line utility used for downloading and uploading data from or to the server.

How do I run a curl command in cmd?

Invoke curl.exe from a command window (in Windows, click Start > Run and then enter "cmd" in the Run dialog box). You can enter curl --help to see a list of cURL commands.

How do I run a curl command in PowerShell script?

The conclusion is that if you need to use the curl (as same as in the Windows command prompt) in PowerShell then you need to call the curl executable( curl.exe ) directly. Else, you should stick to the PowerShell curl alias which resolves to the Invoke-WebRequest cmdlet under the hood.


1 Answers

Just read the error message:

Invoke-WebRequest Cannot bind parameter 'Headers'. Cannot convert the "Content-Type: application/json" value of type "System.String" to type "System.Collections.IDictionary".

In PowerShell, curl is an alias for the Invoke-WebRequest cmdlet. As the error points it out, the Header parameter must be a IDictionary, not a string. This is how it looks like in PowerShell:

@{"Content-Type"= "application/json"}

Some parameters are also different. This is how I would script the request:

Invoke-WebRequest `
    -Uri "http://localhost:5678/api/findgen" `
    -Headers @{"Content-Type"= "application/json"} `
    -Body '{"a": "Val 1","b": "Val 2","c": "Val 3","d": "Val 4"}' `
    -OutFile "file.json" `
    -Method Post
like image 51
Martin Brandl Avatar answered Sep 25 '22 00:09

Martin Brandl