Modern IT environments rely heavily on REST APIs to communicate with web services, cloud platforms, and third-party applications. While pure Batch scripting does not have a native "HTTP Request" command, modern Windows 10 and 11 environments include powerful built-in tools like curl.exe and PowerShell's Invoke-RestMethod.
In this guide, we will demonstrate how to send GET and POST requests to a REST API directly from a Batch script using these native tools.
Method 1: Using the Native curl Command
Starting with Windows 10 version 1803, curl.exe is included as a native system tool. It is the most robust and widely recognized command-line HTTP client.
Making a GET Request
To retrieve data from an API (e.g., getting a fake JSON response):
@echo off
setlocal
set "apiUrl=https://jsonplaceholder.typicode.com/todos/1"
echo Fetching data from: %apiUrl%
echo.
:: Use curl.exe to fetch the JSON response
:: -s = silent (no progress bar), -f = fail silently on HTTP errors
curl.exe -s -f "%apiUrl%"
if %errorlevel% NEQ 0 (
echo.
echo [ERROR] Request failed with error code: %errorlevel%
)
echo.
pauseMaking a POST Request with Headers
To send JSON data to an API, you must specify the HTTP Method (-X POST), provide the Headers (-H), and include the Data payload (-d).
@echo off
setlocal
set "apiUrl=https://jsonplaceholder.typicode.com/posts"
:: Define JSON payload in a file to avoid escaping issues
(
echo {
echo "title": "Batch Test",
echo "body": "This is a test from CMD",
echo "userId": 1
echo }
) > "%TEMP%\payload.json"
echo Sending POST request to %apiUrl%...
echo.
:: Send request using payload file (@) instead of inline string
curl.exe -s -f -X POST "%apiUrl%" -H "Content-Type: application/json" -d @"%TEMP%\payload.json"
if %errorlevel% NEQ 0 (
echo.
echo [ERROR] Request failed with error code: %errorlevel%
)
:: Clean up temp file
del "%TEMP%\payload.json" 2>nul
echo.
pauseCapturing the Response into a Variable
To use the API response in subsequent Batch logic, redirect curl's output to a temporary file and read it back:
@echo off
setlocal enabledelayedexpansion
set "apiUrl=https://jsonplaceholder.typicode.com/todos/1"
set "tempFile=%TEMP%\api_response.json"
:: Fetch and save response
curl.exe -s -f "%apiUrl%" -o "%tempFile%"
if %errorlevel% NEQ 0 (
echo [ERROR] Request failed.
pause
exit /b
)
:: Read each line of the response
echo Response:
for /f "usebackq delims=" %%A in ("%tempFile%") do (
echo %%A
)
:: Clean up
del "%tempFile%" 2>nul
pauseMethod 2: Using the PowerShell Bridge
If curl.exe is not available, or if you need to perform complex JSON parsing after receiving the response, bridging to PowerShell is highly effective. PowerShell's Invoke-RestMethod automatically parses JSON into usable objects.
Fetching and Parsing JSON with PowerShell
@echo off
setlocal
set "apiUrl=https://jsonplaceholder.typicode.com/users/1"
echo Querying API via PowerShell...
:: Fetch data and extract just the 'name' property
for /f "usebackq delims=" %%A in (`
powershell -NoProfile -Command "(Invoke-RestMethod -Uri '%apiUrl%').name"
`) do set "userName=%%A"
if not defined userName (
echo [ERROR] Failed to retrieve user name.
pause
exit /b
)
echo.
echo User Name Retrieved: %userName%
pauseSending a POST Request with PowerShell
@echo off
setlocal
set "apiUrl=https://jsonplaceholder.typicode.com/posts"
for /f "usebackq delims=" %%A in (`
powershell -NoProfile -Command ^
"$body = @{title='Batch Test'; body='Test from CMD'; userId=1} | ConvertTo-Json; " ^
"(Invoke-RestMethod -Uri '%apiUrl%' -Method Post -ContentType 'application/json' -Body $body).id"
`) do set "newId=%%A"
echo Created post with ID: %newId%
pauseWhy Interact with REST APIs in Batch?
- Cloud Integration: Triggering a CI/CD pipeline build, sending a message to a Slack/Discord webhook, or creating a JIRA ticket.
- System Monitoring: Pulling the current status of a web service before proceeding with a local maintenance script.
- Data Ingestion: Downloading the latest configuration file or threat-intelligence list from a remote server before a scan.
Important Considerations
Conclusion
Interacting with REST APIs transforms your Batch scripts from isolated local tasks into powerful, connected automation tools. Whether you use the robust, industry-standard curl.exe or leverage the parsing power of the PowerShell bridge, you can integrate your local Windows environment with virtually any modern web service, cloud platform, or notification system.