The command
curl -v -H "Connection: close" https://httpbin.org/get
What the flags do
| Flag | What it does |
|---|---|
-H "Connection: close" | Ask the server to close the connection after this response (HTTP/1.1). |
-H "Connection: keep-alive" | Redundant on HTTP/1.1, where persistence is already the default. |
--no-keepalive | Turn off TCP keepalive probes. Does not affect HTTP connection reuse. |
--keepalive-time N | Seconds of idleness before the first TCP probe. |
HTTP persistent connections
In HTTP/1.1 a connection stays open after a response unless one side says Connection: close. Curl reuses it, but only within a single invocation: give one command several URLs on the same host and the second request skips the TCP and TLS handshakes. Run curl twice and each process starts from nothing. -v shows it happening with a "Re-using existing connection" line.
# one process, two requests, one connection
curl -v -o /dev/null -o /dev/null \
https://httpbin.org/get https://httpbin.org/headers 2>&1 | grep -i -E "re-using|connected to"
Forcing a close
Sending Connection: close tells the server you will not reuse the connection, and the server echoes the header and closes after responding. It is useful when testing how a load balancer distributes fresh connections, or when a broken server leaves a kept-alive connection hanging.
HTTP/2 and HTTP/3 have no such header. Connection-level headers are forbidden there and curl does not send them; the connection is managed by the protocol itself. Add --http1.1 if you need this behaviour against a server that would otherwise negotiate HTTP/2.
curl -sI --http1.1 -H "Connection: close" https://httpbin.org/get | grep -i "^connection"
TCP keepalive is something else
--no-keepalive, --keepalive-time and friends configure operating-system probes sent on an idle TCP socket so that firewalls and NAT devices do not silently drop it. They matter for long transfers with quiet periods, such as a slow download or a streaming response, and have nothing to do with whether the HTTP connection is reused.
curl --keepalive-time 30 -o big.iso https://example.com/big.iso
Common mistakes
- A shell loop that runs curl repeatedly never reuses connections. Pass all the URLs to one command, or use
--parallel, or a config file with-K. - The
Keep-Alive: timeout=5response header is advisory. Servers close idle connections whenever they like. Connection: keep-alivedoes not make a server keep a connection open if it is configured to close it.
Related curl examples
Curl Close Connection Header Example
Connection: close asks the other side to hang up once the response is complete.
Curl Send Header Example
-H adds a header line to the request.
Curl Timeout Example
By default curl puts no limit on how long a transfer may take, so one silent server can hang a script indefinitely.
Curl No Cache Example
Curl keeps no cache of its own, so a stale response always comes from something in between: a CDN, a reverse proxy, a corporate gateway.
All Curl Examples
49 worked examples, grouped by what you are trying to do.
More under Headers
References
- curl man page: --no-keepalive — Turning off TCP keepalive probes.
- MDN: Keep-Alive — The response header that advertises connection limits.
These links leave api-tester.com, which is independent of the projects they point to.
FAQ
Does curl use keep-alive by default?
Yes. With HTTP/1.1 curl keeps the connection open and reuses it for further requests to the same host within the same command.
How do I send Connection: close with curl?
Add -H "Connection: close". On servers that negotiate HTTP/2, also add --http1.1, because HTTP/2 does not use the Connection header.
What does --no-keepalive do in curl?
It disables TCP keepalive probes on the socket. It does not disable HTTP persistent connections.