Home > database >  cURL runs fine on cmd line but not with a batch script
cURL runs fine on cmd line but not with a batch script

Time:09-10

How do i convert this cURL cmd into a batch script -

curl --request POST \
--url https://api.example.com \
--header "Authorization: Bearer token" \
--header 'Content-Type: application/json' \
--data '{"personalizations": [{"to": [{"email": "[email protected]"}]}],"from": {"email": "[email protected]"},"subject": "Subject","content": [{"type": "text/plain", "value": "Body"}]}'

this is the error that i am getting -

[Running] cmd /c "c:\Users\sendMail.bat"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: \
'--url' is not recognized as an internal or external command,
operable program or batch file.
'--header' is not recognized as an internal or external command,
operable program or batch file.
'--header' is not recognized as an internal or external command,
operable program or batch file.
'--data' is not recognized as an internal or external command,
operable program or batch file.

[Done] exited with code=1 in 0.078 seconds

CodePudding user response:

You add cmd.
You just need to open a CMD window to execute your curl.
And needs to all be on one line.
Those errors you are getting because it is trying to execute each line as a command.

cmd /k 'curl -X POST --url https://api.example.com -H "Authorization: Bearer $token" -H "Content-Type: application/json" -d {"personalizations": [{"to": [{"email": "[email protected]"}]}],"from": {"email": "[email protected]"},"subject": "Subject","content": [{"type": "text/plain", "value": "Body"}]}';
PAUSE

When you are done testing, remove the PAUSE.

CodePudding user response:

Escape a new line with a ^ instead of a \ and for --data turn ' into " and " into \":

curl --request POST ^
--url https://api.example.com ^
--header "Authorization: Bearer token" ^
--header "Content-Type: application/json" ^
--data "{\"personalizations\": [{\"to\": [{\"email\": \"[email protected]\"}]}],\"from\": {\"email\": \"[email protected]\"},\"subject\": \"Subject\",\"content\": [{\"type\": \"text/plain\", \"value\": \"Body\"}]}"
  • Related