Keep-Alive Connection means the server will not close the Connection after making the request. To pass the Connection: keep-alive header to Curl, you can use the -H command-line option. Clients may specify the persistent connection options in a separate - H "Keep-Alive: [options]" HTTP header. In this Curl Keep-AliveConnection Example, we are sending a request to the ReqBin echo URL. Click Run to execute the Curl Command with Keep-Alive Connection Header online and see the results.
The Connection header determines whether the current TCP connection remains open after a transaction. Persistent connections allow you to send multiple requests without opening a new connection for each transaction. By default, connections are persistent for HTTP/1.1 clients. For HTTP/1.0 connections, you need to pass the "Connection: keep-alive" request header to indicate that you want a persistent connection.
Connection Header Syntax
-H "Connection: keep-alive"
What is Keep-Alive header?
The Keep-Alive header allows you to set the parameters of a persistent connection: connection timeout and the maximum number of requests in one connection.
Keep-Alive header Example
-H "Keep-Alive: timeout=5, max=100"
timeout: sets the maximum timeout to 5 seconds
max: sets the maximum number of requests per connection to 100
How to make a Keep-Alive request with Curl?
An example of sending a Curl request with directives for establishing a persistent connection and with connection parameters:
The table shows the difference between persistent connections in HTTP versions.
In HTTP/1.0, connections are not considered persistent unless the client sends -H "Connection: keep-alive" header with the request. When the server receives a persistent connection request, it responds by sending a "Connection: keep-alive" header to the client. After that, the connection will be open, and the client sends other requests using the same connection. The connection will remain open until the client or server drops the connection.
In HTTP/1.1, all connections are considered persistent unless otherwise specified and do not require additional -H "Connection: keep-alive" headers to establish a persistent connection.
Connection and Keep-Alive headers are not allowed in HTTP/2. Firefox and Chrome ignore these headers in HTTP/2 responses, but Safari will not load the web page if the server returns these headers in error.