cURL examples: GET and POST requests with JSON on Linux, Windows and PowerShell
Most curl mistakes come down to quotes and a forgotten Content-Type header. Below are commands you can copy and point at your own URL.
A GET request and the status code
The -i option shows the status code and headers, and -s hides the progress meter. To get only the status code, use -o and -w.
curl -i https://api.example.com/orders/42
curl -s -o /dev/null -w "%{http_code}\n" https://api.example.com/orders/42POST with JSON on Linux and macOS
The body goes in -d, and the Content-Type: application/json header is required: without it many servers will not recognize the format. In bash it is easiest to wrap the JSON in single quotes.
curl -X POST https://api.example.com/orders \
-H "Content-Type: application/json" \
-d '{"sku":"A-1","qty":2}'The --json option
Since curl 7.82 there is a --json option: it sets the JSON Content-Type and Accept headers itself and sends the request as POST.
curl --json '{"sku":"A-1","qty":2}' https://api.example.com/ordersPOST on Windows: cmd and PowerShell
Single quotes do not work in cmd: wrap the JSON in double quotes and escape the inner quotes with a backslash. In Windows PowerShell 5.1, curl is an alias for Invoke-WebRequest, so call curl.exe. The most reliable way on any system is to put the JSON in a file and pass it with @.
REM cmd
curl -X POST https://api.example.com/orders -H "Content-Type: application/json" -d "{\"sku\":\"A-1\",\"qty\":2}"
# PowerShell and any system: JSON from the file body.json
curl.exe -X POST https://api.example.com/orders -H "Content-Type: application/json" -d "@body.json"A token and other headers
Each header gets its own -H option. Keep the token in an environment variable rather than in the command: commands stay in the terminal history.
curl https://api.example.com/orders/42 \
-H "Authorization: Bearer $TOKEN" \
-H "Accept: application/json"How to check the response, not just send it
curl shows the response but not whether it is correct. A copied cURL command can be imported into Checkcraft: the request is saved and the response gets rules for status, fields, types and totals. The cURL and OpenAPI import guide explains how.
Repeat this on your own API
Checkcraft is a desktop app that saves API response checks without code and shows what did not match. Start with a sample, then use your own sanitized data. Free runs on Windows and Linux with no time limit.
Download FreeDownload and first runNo sign-up or email needed, the archive is ready in a couple of minutes. See the edition limits.
References
Start with the linked example and compare its result with your own test requirement.