curl
Make an HTTP request — GET, POST, a download, an API call — and print the response, with flags for headers, bodies, redirects and errors.
curl -fsSL <url>Angle brackets are yours to fill in. Windows, Linux, macOS.
Examples for curl
Send a POST request with a JSON body
curl -H "Content-Type: application/json" -d '{"name":"demo"}' <url>-d makes the request a POST, so -X POST is not needed. The Content-Type header tells the server the body is JSON; without it, curl sends application/x-www-form-urlencoded and many APIs reject the body. On curl 7.82 or later, --json does both in one flag. In cmd.exe, single quotes do not group: use double quotes around the body and escape the inner ones as \".
curltool-H "Content-Type: application/json"the body is JSON-d '{"name":"demo"}'the body; implies POST<url>urlSend an Authorization header with a bearer token
curl -H "Authorization: Bearer <token>" <url>Most APIs take their key this way. The token stays in your shell history; read it from a variable, such as "Authorization: Bearer $TOKEN", to keep it out.
curltool-H "Authorization: Bearer <token>"the API key<url>urlFollow redirects
curl -L <url>If curl prints nothing, or a short page that says Moved, the server sent a redirect and curl did not follow it. -L follows it, up to 50 hops. A URL that changes from http to https is the usual cause.
curltool-Lfollow redirects<url>urlDownload a file
curl -LO <url>-O saves the file under the name at the end of the URL, in the current directory. To choose the name, use -o <file> instead. -L matters here: download links often redirect to a CDN, and without it you save the redirect page.
curltool-LOfollow redirects, save under the URL's name<url>urlShow only the response headers
curl -I <url>-I sends a HEAD request, so the server sends no body. Some servers answer HEAD differently from GET; to see the headers of a real GET, use -i, which prints the headers and then the body.
curltool-Iheaders only<url>urlPrint only the HTTP status code
curl -s -o /dev/null -w "%{http_code}" <url>-o /dev/null discards the body, -s hides the progress meter, and -w prints the status code after the transfer. Useful in a script or a health check. On Windows, use -o NUL.
curltool-squiet-o /dev/nulldiscard the body-w "%{http_code}"print the status<url>urlUpload a file as a form
curl -F "file=@<path>" <url>Sends multipart/form-data, as an HTML file input does. The @ tells curl to read the file; without it, the server receives the path as text. Change file to the field name the server expects.
curltool-F "file=@<path>"the form field and the file<url>urlLog in with a username and password
curl -u <user> <url>Sends HTTP basic authentication. With only the user name, curl asks for the password, so it does not go into your shell history. You can write -u <user>:<password> in a script, but anyone who can read the script can read the password.
curltool-u <user>asks for the password<url>urlSee why a request fails
curl -v <url>Prints each step: the DNS lookup, the connection, the TLS handshake, then the request headers (lines starting >) and the response headers (lines starting <). A certificate error, a wrong Host header or an unexpected redirect shows here.
curltool-vshow the whole exchange<url>urlStop a request that takes too long
curl --max-time 10 <url>curl has no overall time limit by default, so a server that stops responding can hold a script open indefinitely. --max-time limits the whole transfer; --connect-timeout limits only the connection.
curltool--max-time 10give up after 10 seconds<url>urlSkip the SSL certificate check
curl -k <url>Lets you reach a server with a self-signed or expired certificate, to confirm that the certificate is the problem. Fix the certificate, or give curl the right CA with --cacert <file>, rather than leaving -k in place.
With -k, curl cannot tell the real server from an impostor. Never use it in a script or with a password.
curltool-kno certificate check<url>urlUse the real curl in Windows PowerShell
curl.exe -I <url>In Windows PowerShell 5.1, curl is an alias for Invoke-WebRequest, which rejects curl's flags. curl.exe calls the real curl, which Windows 10 and later include. PowerShell 7 has no alias, so plain curl works there.
curl.exenot the PowerShell alias-Iheaders only<url>url
Flags
- -L --location
- Follow redirects. Without it, curl prints the 301 or 302 and stops, which looks like an empty response.
- -H --header <name>: <value>
- Add a request header. Repeat it once per header.
- -d --data <body>
- Send a request body. Makes the request a POST unless -X says otherwise. Use @<file> to send a file's contents.
- --json <body>
- Send a JSON body and set both the Content-Type and Accept headers to application/json. Needs curl 7.82 or later.
- -X --request <method>
- Use another HTTP method, such as PUT, PATCH or DELETE.
- -F --form <name>=<value>
- Send a multipart form field. Prefix the value with @ to upload a file, as a browser form does.
- -o --output <file>
- Write the response body to <file> instead of the terminal.
- -O --remote-name
- Save the download under the file name in the URL.
- -I --head
- Send a HEAD request and print only the response headers.
- -i --include
- Print the response headers before the body.
- -s --silent
- Hide the progress meter and error messages. Pair it with -S to keep the errors.
- -S --show-error
- Print an error message even when -s is on.
- -f --fail
- Exit with an error on an HTTP status of 400 or above, instead of printing the error page as if it were the result.
- -v --verbose
- Print the connection, TLS handshake, and the request and response headers. The first thing to add when a request fails.
- -u --user <user>:<password>
- Send HTTP basic authentication. Give only <user> and curl asks for the password.
- -w --write-out <format>
- Print details about the transfer after it completes, such as %{http_code} or %{time_total}.
- -m --max-time <seconds>
- Stop the whole transfer after <seconds>, however far it got.
- -k --insecure
- Skip TLS certificate verification.Anyone between you and the server can read and change the traffic. Use it only to diagnose a certificate problem, never in a script.