Skip to lesson

Sending requests with curl

A new endpoint has no screen yet. One command in the terminal sends a real request and shows the whole answer, so developers use it to test an API and to share calls.

Installing and checking curl

curl is a small command-line program that sends one HTTP request and prints the response. It behaves the same way on every computer and server, which makes it the common language for sharing and testing API calls.

Check that curl runs. In Windows PowerShell the first command below fails, because curl there means a different command; curl.exe is the real program.

Example 1 · PowerShell: the curl alias versus curl.exe

Terminal
curl -X POST http://localhost:8080/echocurl.exe --version
Sample output (Windows PowerShell 5.1)
PS C:\Users\Asha> curl -X POST http://localhost:8080/echo
Invoke-WebRequest : A parameter cannot be found that matches parameter name 'X'.
At line:1 char:6
+ curl -X POST http://localhost:8080/echo
+      ~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
PS C:\Users\Asha> curl.exe --version
curl 8.21.0 (Windows) libcurl/8.21.0 Schannel zlib/1.3.2 WinIDN WinLDAP
Release-Date: 2026-06-24
Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns ldap ldaps mqtt mqtts pop3 pop3s smtp smtps telnet tftp ws wss
Features: alt-svc AsynchDNS HSTS HTTPS-proxy IDN IPv6 Kerberos Largefile libz SPNEGO SSL SSPI threadsafe Unicode UnixSockets

What this code does

  • In Windows PowerShell, curl runs Invoke-WebRequest, which has no -X option, so the command fails before any request is sent.
  • curl.exe names the real program. Its first line shows the version (8.21.0 here); yours may differ. Anything from 7.82 up supports every option on this page.

curl comes with Windows 10 (version 1803 and later), Windows 11 and macOS, so there is usually nothing to install. curl --version prints the version; the --json option used later needs 7.82 or newer. In Windows PowerShell (the blue 5.1 version) always type curl.exe; Command Prompt, Git Bash and macOS Terminal accept plain curl.

If curl.exe --version is not found, update Windows or use Git Bash, which brings its own curl.

A GET request; -i for headers, -v for everything

-i and -v are curl options that show more than the body. -i (include) adds the status line and the response headers; -v (verbose) also shows the request that curl sent.

Plain curl URL sends a GET and prints only the body. The same product three ways: body only, with -i, and with -v.

Example 2 · Body only, -i and -v

Terminal
curl http://localhost:8080/api/v1/products/6curl -i http://localhost:8080/api/v1/products/6curl -v http://localhost:8080/api/v1/products/6
Sample output
$ curl http://localhost:8080/api/v1/products/6
{"id":6,"sku":"ACC-AD141","name":"boAt Airdopes 141","categoryId":4,"categoryName":"Accessories","price":1299.00,"stock":120,"isActive":true}
$ curl -i http://localhost:8080/api/v1/products/6
HTTP/1.1 200 OK
Date: Fri, 16 Oct 2026 02:47:20 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 141
ETag: "p6-v1"
Cache-Control: no-cache

{"id":6,"sku":"ACC-AD141","name":"boAt Airdopes 141","categoryId":4,"categoryName":"Accessories","price":1299.00,"stock":120,"isActive":true}
$ curl -v http://localhost:8080/api/v1/products/6
* Host localhost:8080 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
*   Trying [::1]:8080...
* Established connection to localhost (::1 port 8080) from ::1 port 60178 
* using HTTP/1.x
> GET /api/v1/products/6 HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.21.0
> Accept: */*
> 
* Request completely sent off
< HTTP/1.1 200 OK
< Date: Fri, 16 Oct 2026 02:47:21 GMT
< Content-Type: application/json; charset=utf-8
< Content-Length: 141
< ETag: "p6-v1"
< Cache-Control: no-cache
< 
* Connection #0 to host localhost:8080 left intact
{"id":6,"sku":"ACC-AD141","name":"boAt Airdopes 141","categoryId":4,"categoryName":"Accessories","price":1299.00,"stock":120,"isActive":true}

What this code does

  • Plain curl prints just the JSON. -i adds the status line, the headers and a blank line before the same body.
  • -v adds the request too: the > lines show that curl sent GET, Host, User-Agent and Accept: */* by itself.

In the -v output, lines that start with > are the request curl sent, < is the response head and * are curl's own notes. Use -i to check status codes and headers, and -v to see exactly what went out.

No practice API on your computer yet? https://httpbin.org/anything sends your request back as JSON, and https://jsonplaceholder.typicode.com/posts/1 returns a sample item; their output differs a little from the practice API shown here.

-X, -H, -d and --json

--json is a curl option (curl 7.82 and newer) that sends its text as a JSON body and sets both Content-Type: application/json and Accept: application/json. It means POST unless -X says otherwise.

The same PATCH sent two ways: with -X, -H and -d, then with --json alone.

Example 3 · -X, -H and -d versus --json

Terminal
curl -s http://localhost:8080/echo -X PATCH -H "Content-Type: application/json" -d '{"stock":75}'curl -s http://localhost:8080/echo -X PATCH --json '{"stock":75}'
Sample output
$ curl -s http://localhost:8080/echo -X PATCH -H "Content-Type: application/json" -d '{"stock":75}'
PATCH /echo HTTP/1.1
Host: localhost:8080
User-Agent: curl/8.21.0
Accept: */*
Content-Type: application/json
Content-Length: 12

{"stock":75}
$ curl -s http://localhost:8080/echo -X PATCH --json '{"stock":75}'
PATCH /echo HTTP/1.1
Host: localhost:8080
User-Agent: curl/8.21.0
Content-Type: application/json
Accept: application/json
Content-Length: 12

{"stock":75}

What this code does

  • Both commands send the same PATCH with the same 12-byte body. /echo on the practice API returns the request exactly as it arrived.
  • With -d you add Content-Type yourself and Accept stays */*. --json sets Content-Type and Accept to application/json in one option.

Four options build almost every API request:

  • -X METHOD sets the method: -X PUT, -X PATCH, -X DELETE. Capital X: lowercase -x sets a proxy server.
  • -H "Name: value" adds a header. Repeat it for more headers. A header without the colon is silently dropped.
  • -d 'data' sends a body and switches the method to POST. Its default Content-Type is form data, so add -H "Content-Type: application/json" for JSON.
  • --json 'data' sends a JSON body with both JSON headers.

Do not add -X POST when you use -d or --json; it is already a POST. And never write -X GET with -d: that sends a GET with a body, which servers ignore or reject.

Fix the error

Fix the curl command

Read the real message, then pick the line that fixes the command.

Round 1 of 2

Change the stock of a product with a PATCH.

curl -s http://localhost:8080/echo \
-x PATCH \
-H "Content-Type: application/json" \
-d '{"stock":75}'

Error

curl: (5) Could not resolve proxy: PATCH

Quoting JSON, and bodies from a file

@file is a curl argument that reads the request body from a file: --json @product.json or -d @product.json. The file needs no quote escaping and works the same in every shell.

In Windows PowerShell 5.1 inline JSON loses its inner quotes, but a file arrives whole. The first command below breaks; the second works.

Example 4 · PowerShell 5.1 breaks inline JSON; a file works

Terminal
curl.exe -s http://localhost:8080/echo --json '{"name":"Tripod 12in","price":449.00}'curl.exe -s http://localhost:8080/echo --json "@product.json"
Sample output (Windows PowerShell 5.1)
PS C:\Users\Asha> curl.exe -s http://localhost:8080/echo --json '{"name":"Tripod 12in","price":449.00}'
POST /echo HTTP/1.1
Host: localhost:8080
User-Agent: curl/8.21.0
Content-Type: application/json
Accept: application/json
Content-Length: 31

{name:Tripod 12in,price:449.00}
PS C:\Users\Asha> curl.exe -s http://localhost:8080/echo --json "@product.json"
POST /echo HTTP/1.1
Host: localhost:8080
User-Agent: curl/8.21.0
Content-Type: application/json
Accept: application/json
Content-Length: 82

{"sku":"ACC-TRI12","name":"Tripod 12in","categoryId":4,"price":449.00,"stock":15}

What this code does

  • The first body arrives as {name:Tripod 12in,price:449.00}: PowerShell 5.1 removed every inner double quote, and a JSON API would answer 400.
  • The second command reads product.json from the current folder. The "@product.json" quotes stop PowerShell from treating @ as its own syntax, and the JSON arrives untouched, including the file's final line break (82 bytes).

Example 5 · Create a product from a file

Terminal
curl -i http://localhost:8080/api/v1/products --json @product.json
Sample output
HTTP/1.1 201 Created
Date: Fri, 16 Oct 2026 02:47:32 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 134
Location: http://localhost:8080/api/v1/products/16

{"id":16,"sku":"ACC-TRI12","name":"Tripod 12in","categoryId":4,"categoryName":"Accessories","price":449.00,"stock":15,"isActive":true}

What this code does

  • product.json holds {"sku":"ACC-TRI12","name":"Tripod 12in","categoryId":4,"price":449.00,"stock":15}. In Git Bash and macOS Terminal, the bare @product.json works.
  • The file is the body, --json adds the headers, and the API answers 201 Created. Keeping request bodies in files also lets you commit them next to your scripts.

Quoting rules differ by shell. In macOS Terminal and Git Bash wrap the JSON in single quotes: --json '{"stock":75}'. Command Prompt needs double quotes with a backslash before each inner quote. Windows PowerShell 5.1 strips the inner quotes, so use a file and quote the @ part: --json "@product.json"; PowerShell 7.3 and later pass the quotes correctly.

--json @file sends the file exactly as it is, while -d @file removes its line breaks. Keeping request bodies in files also lets you commit them next to your scripts.

-L to follow redirects; -s and -o for clean output

-L (location) is a curl option that follows a 3xx redirect to the URL in the Location header, again and again, until it reaches a final answer. Without it, curl stops at the redirect.

Fetch a page that redirects, save the body to a file, then repeat quietly. The first run shows a progress meter; the second prints nothing.

Example 6 · Follow a redirect and save the body quietly

Terminal
curl -o page.json -L https://httpbin.org/redirect/1curl -s -o page.json -L https://httpbin.org/redirect/1cat page.json
Sample output (Windows curl; times, origin address and trace id differ)
$ curl -o page.json -L https://httpbin.org/redirect/1
  % Total    % Received % Xferd  Average Speed  Time    Time    Time   Current
                                 Dload  Upload  Total   Spent   Left   Speed
100    215 100    215   0      0    178      0   00:01   00:01              0
100    256 100    256   0      0    103      0   00:02   00:02            203
$ curl -s -o page.json -L https://httpbin.org/redirect/1
$ cat page.json
{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/8.21.0", 
    "X-Amzn-Trace-Id": "Root=1-6ad1dc6f-6f88759675fb48ca797fa203"
  }, 
  "origin": "203.0.113.25", 
  "url": "https://httpbin.org/get"
}

What this code does

  • /redirect/1 answers with a 302 redirect to /get; -L follows it. -o page.json writes the body to the file, so curl prints the progress meter instead: one row for the redirect (its small 215-byte HTML note) and one for the final JSON.
  • With -s the same command prints nothing at all. cat (use type in Command Prompt or Get-Content in PowerShell) shows the saved JSON.

-o file saves the body to a file instead of printing it, and curl then shows a progress meter (a table of bytes and speed). -s (silent) hides the meter and curl's own error messages, which keeps scripts and copied output clean. Combine short options as you like: -sL, -si, -sLo page.json; add -S (-sS) to stay quiet but still see real errors.

Next step

Check your understanding

12 quick questions