The command
curl -i --http1.1 -H "Connection: close" https://httpbin.org/get
What the flags do
| Flag | What it does |
|---|---|
-H "Connection: close" | Tell the server the connection will not be used again. |
--http1.1 | Needed where the server would otherwise negotiate HTTP/2, which has no such header. |
-0, --http1.0 | HTTP/1.0 closes after every response unless asked not to. |
-w "%{num_connects}" | Report how many new connections a transfer had to open. |
Proving the connection was closed
Give one curl command two URLs on the same host and count connections. Normally the first transfer opens one and the second opens none, because it travels on the connection already there. Add the header and both transfers report a new connection. The write-out string is printed once per transfer, so the two numbers appear on separate lines.
# reused: prints 1, then 0
curl -s --http1.1 -o /dev/null -o /dev/null -w "new connections: %{num_connects}\n" \
https://httpbin.org/get https://httpbin.org/headers
# closed each time: prints 1, then 1
curl -s --http1.1 -o /dev/null -o /dev/null -w "new connections: %{num_connects}\n" \
-H "Connection: close" https://httpbin.org/get https://httpbin.org/headers
When the header matters
Each curl process begins with no open connections, so a shell loop that starts curl again and again gets a fresh one every time regardless. There the header only lets the server release its socket at once instead of waiting for an idle timer. It earns its place when a single command makes many requests: a URL range, several URLs, or --parallel.
That makes it a way to reproduce what a client without connection pooling would do. Two uses are common: watching how a load balancer spreads brand-new connections, and ruling out the intermittent "empty reply" errors that happen when a server drops an idle connection just as the client reuses it.
# five requests in one process, each on its own connection
curl -s --http1.1 -H "Connection: close" -o /dev/null \
-w "%{remote_ip} connect=%{time_connect}s\n" "https://httpbin.org/get?n=[1-5]"
Proxies, servers and HTTP/2
The header is hop-by-hop: it governs only the link between you and the next machine. Behind a CDN or reverse proxy, your connection to the proxy closes while the proxy's pooled connection to the origin carries on, and the origin never learns you asked. Servers send the header on their own initiative too, after a set number of requests or during a shutdown, and curl obeys by opening a new connection for the next URL.
HTTP/2 and HTTP/3 manage connections inside the protocol, and the header has no meaning there. Verbose output may still list it among the request lines, so do not take that as proof it had an effect; force --http1.1 and look for the server's own reply.
curl -v --http1.1 -o /dev/null -H "Connection: close" https://httpbin.org/get 2>&1 \
| grep -i -E "^< connection|closing connection"
Common mistakes
- It is not a timeout. The header ends the connection after a response; it does nothing about a response that never arrives. Use
--max-timefor that. - Closing every connection is slow at volume. Each request repeats the TCP and TLS handshakes, which often cost more than the request itself.
- A proxy may answer on the server's behalf. Seeing the header echoed back tells you about the nearest hop only.
Related curl examples
Curl Keep-Alive Example
"Keep-alive" names two unrelated mechanisms, and curl has switches for both.
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.
Convert Curl to a Raw HTTP Request
Every curl command turns into a few lines of text sent over a socket.
All Curl Examples
49 worked examples, grouped by what you are trying to do.
More under Headers
References
- curl man page: -H — Adding the Connection request header.
- MDN: Connection — The hop-by-hop header and why HTTP/2 forbids it.
These links leave api-tester.com, which is independent of the projects they point to.
FAQ
How do I close the connection after a curl request?
Send the header -H "Connection: close", adding --http1.1 for servers that would otherwise use HTTP/2. Curl also closes every connection when the command exits.
What does the Connection: close header do?
It tells the other side that the connection will not be reused. The server sends its response, echoes the header and closes the TCP connection.
Does curl close the connection automatically?
Yes, when the curl process ends. During a single command with several URLs it keeps connections open and reuses them unless Connection: close is sent.