Skip to main content

How to Post Data to a Webhook URL from a Batch Script

A Webhook is an HTTP endpoint designed to receive automatic payloads from external systems. Webhooks are the backbone of modern integrations (like Zapier, Make, and custom APIs), allowing independent systems to trigger workflows simply by receiving a structured piece of data. In Batch scripting, natively posting to a webhook is achieved rapidly via the curl.exe command.

In this guide, we will demonstrate how to assemble and POST structured data to a webhook endpoint.

The Strategy: Build the Payload, Then Post

  1. Identify the destination URL (e.g., https://webhook.site/YOUR_UNIQUE_ID).
  2. Gather system metrics or variables you wish to transmit (e.g., %USERNAME%, %COMPUTERNAME%).
  3. Format this data as a JSON string (the industry standard).
  4. Use curl.exe to transmit the data via a POST request.

Implementation Script

@echo off
setlocal enabledelayedexpansion

:: 1. Define the Webhook Endpoint
set "webhookUrl=https://webhook.site/YOUR_UNIQUE_ID_HERE"

:: 2. Gather Local Variables
set "hostInfo=%COMPUTERNAME%"
set "userInfo=%USERNAME%"

:: Optional: Get the current date and time
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /value') do set "dt=%%I"
set "timestamp=!dt:~0,4!-!dt:~4,2!-!dt:~6,2! !dt:~8,2!:!dt:~10,2!:!dt:~12,2!"

:: 3. Assemble the JSON Payload
:: Careful escaping is required for the internal double-quotes (\")
set "json={\"hostname\": \"%hostInfo%\", \"user\": \"%userInfo%\", \"event_time\": \"!timestamp!\", \"status\": \"Script Execution Complete\"}"

echo Posting data to webhook: %webhookUrl%
echo.

:: 4. Transmit with HTTP POST
:: Use curl.exe explicitly to avoid the PowerShell alias conflict
:: -s = silent mode
:: -o nul = discard response body
:: -w "%%{http_code}" = output only the HTTP status code
curl.exe -s -o nul -w "%%{http_code}" -X POST -H "Content-Type: application/json" -d "%json%" "%webhookUrl%" > "%TEMP%\webhook_response.tmp"

:: Capture the exit code immediately
set "curlResult=!errorlevel!"

:: 5. Validate the result
if !curlResult! neq 0 (
echo [ERROR] curl failed with exit code !curlResult!. Check network connectivity.
del "%TEMP%\webhook_response.tmp" 2>nul
pause
exit /b 1
)

:: Read the HTTP status code returned by curl
set /p "httpCode=" < "%TEMP%\webhook_response.tmp"
del "%TEMP%\webhook_response.tmp" 2>nul

:: Check for success (2xx status codes)
if "!httpCode:~0,1!"=="2" (
echo [SUCCESS] Data posted successfully. HTTP status: !httpCode!
) else (
echo [ERROR] Webhook returned HTTP status !httpCode!. Verify your endpoint URL and payload.
pause
exit /b 1
)

endlocal
pause
exit /b 0

Why Post to Webhooks?

  1. Centralized Logging: Having hundreds of workstations push execution success metrics or error codes to a single webhook dashboard.
  2. No-Code Orchestration: A Batch script finishing a local database backup can trigger a "Zapier Zap" or a "Make Scenario" that automatically emails stakeholders or syncs data to an external CRM.
  3. Auditing Executions: Tracking exactly which users ran a privileged script on which machines, effectively creating an immutable trail on an external server.

Advanced Payload Assembly (Multiple Lines)

Creating a massive JSON string on a single line is fragile. For larger payloads containing many variables, write the JSON structure precisely to a temporary file first, then use curl.exe to send the file.

@echo off
setlocal enabledelayedexpansion

set "webhookUrl=https://webhook.site/YOUR_ID_HERE"
set "tempJson=%TEMP%\payload.json"

:: Write multiline JSON cleanly (no backslash escaping needed here)
(
echo {
echo "event": "backup_complete",
echo "server": "%COMPUTERNAME%",
echo "details": {
echo "duration_seconds": 345,
echo "size_mb": 4500
echo }
echo }
) > "%tempJson%"

:: Use the @ symbol to tell curl to read the file contents
:: Use curl.exe explicitly to avoid the PowerShell alias conflict
curl.exe -s -o nul -w "%%{http_code}" -X POST -H "Content-Type: application/json" -d @"%tempJson%" "%webhookUrl%" > "%TEMP%\webhook_response.tmp"

:: Capture the exit code immediately
set "curlResult=!errorlevel!"

:: Cleanup the temporary JSON payload
del "%tempJson%" 2>nul

:: Validate the result
if !curlResult! neq 0 (
echo [ERROR] curl failed with exit code !curlResult!. Check network connectivity.
del "%TEMP%\webhook_response.tmp" 2>nul
pause
exit /b 1
)

:: Read the HTTP status code returned by curl
set /p "httpCode=" < "%TEMP%\webhook_response.tmp"
del "%TEMP%\webhook_response.tmp" 2>nul

:: Check for success (2xx status codes)
if "!httpCode:~0,1!"=="2" (
echo [SUCCESS] Data posted successfully. HTTP status: !httpCode!
) else (
echo [ERROR] Webhook returned HTTP status !httpCode!. Verify your endpoint URL and payload.
pause
exit /b 1
)

endlocal
pause
exit /b 0

Important Considerations

  1. JSON Syntax Errors: Missing quotes, mismatched brackets, or trailing commas will cause the webhook endpoint to reject the payload (400 Bad Request). The file-based approach (Advanced Payload Assembly) significantly reduces these formatting errors compared to escaping strings inline.
  2. Authentication: If your custom webhook endpoint requires an API Key, include it as an additional header: -H "Authorization: Bearer YOUR_API_KEY".
  3. Variable Expansion: Check that none of the system values being injected (like %USERNAME%) contain trailing spaces or characters like ", which will immediately break the JSON formatting unless sanitized first.

Conclusion

Posting data to a webhook connects your local scripts to the broader fabric of internet orchestration. Using the native curl.exe and writing correctly formatted JSON payloads, any Batch script can become a trigger for complex workflows in Zapier, Slack, or custom enterprise middleware securely and instantaneously.