I read various articles and found that the latest versions of Windows support the cURL
command via the command prompt
right out of the box without any installation. So I am trying to make a simple cURL
request to my local web server, but it is unable to make the request and get the response. It may be because the cURL request consists of the request body.
If I try to make the same request via Windows Power Shell
, then it's also not working as expected. So I want to know why I cannot make the cURL request via Command Prompt and Power Shell.
Following is a sample cURL request that I am trying to make:
curl -X 'POST' \
'http://localhost:9010/api/generatePersons?pretty=false' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"persons": [{
"nodeId": 1,
"eventType": "persons",
"personID": false,
"refPersons": [],
"parent": {},
"child": []
}]
}'
CodePudding user response:
curl in PowerShell is not curl.exe, it's alias for the cmdlet Invoke-WebRequest.
Get-Alias -Name curl
# Results
<#
CommandType Name Version Source
----------- ---- ------- ------
Alias curl -> Invoke-WebRequest
#>
To use curl.exe in PS, you must fully qualify it using curl.exe,...
Get-Command -Name curl.exe
# Results
<#
CommandType Name Version Source
----------- ---- ------- ------
Application curl.exe 7.83.1.0 C:\Windows\system32\curl.exe
#>
...so that PS is not using Invoke-WebRequest.
Get-Command -Name curl
# Results
<#
CommandType Name Version Source
----------- ---- ------- ------
Alias curl -> Invoke-WebRequest
#>
As per my comment regarding multi-line cmd.exe code, for example:
Multi-line string in command-line arguments
In case of executables, consider _spawnl and _spawnv, where you can define the list of passed arguments. Example with a single multi-line argument:
_spawnl( _P_WAIT, "D:\\TestProgram.exe", "D:\\TestProgram.exe", "some\r\nargument", NULL);
https://renenyffenegger.ch/notes/Windows/dirs/Windows/System32/cmd_exe/line-continuation
cmd.exe: line continuation
A long command can be spread out over multiple lines if each line's last character is the caret (^) character. The following echo command prints the entire text on one single line:
echo ^
Hello world, ^
the number is ^
42.
CodePudding user response:
This is a Win 10 CMD command line answer.
curl -X 'POST' -H "Content-Type: application/json" -H "accept: application/json" http://localhost:9010/api/generatePersons?pretty=false' -d '{ "persons": [{ "nodeId": 1, "eventType": "persons", "personID": false, "refPersons": [], "parent": {}, "child": [] }]}'
The ?pretty=false
is not right. A query string in the URL works for a GET or PUT request. It is not kosher in a POST. It does get submitted okay.
If it were a PHP API receiving this curl to get the pretty
,use $_GET[]
or $_REQUEST[]
.
The issues you had were Microsoft induced.
- Do not enclose the URL in quotes
- Use double (not single) quotes for the headers
This should work for you:
curl -X 'POST' -H "Content-Type: application/json" -H "accept: application/json" http://localhost:9010/api/generatePersons?pretty=false -d '{ "persons": [{ "nodeId": 1, "eventType": "persons", "personID": false, "refPersons": [], "parent": {}, "child": [] }]}'