Curl Command With Keep-Alive Connection Header

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.
Curl Command With Keep-Alive Connection Header Run
     -H "Connection: keep-alive"
     -H "Keep-Alive: timeout=5, max=100"
Updated: Viewed: 13196 times

What is Curl?

Curl is a command-line utility available for Linux, Windows, and macOS, and that allows sending HTTP requests to the server with different parameters and methods. Curl supports over 25+ protocols, including HTTP, HTTPS, FTP, and has built-in support for HTTP Cookies, web forms, SSL, user authentication, and proxies.

What is Connection Header?

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:

Keep-Alive Connection Example
   -H "Connection: keep-alive"
   -H "Keep-Alive: timeout=5, max=100"

Persistent connection in the HTTP versions

The table shows the difference between persistent connections in HTTP versions.

HTTP version Description
HTTP/1.0 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.
HTTP/1.1 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.
HTTP/2 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.

See also

Generate Code Snippets for Curl Keep Alive Connection Header Example

Convert your Curl Keep Alive Connection Header request to the PHP, JavaScript/AJAX, Curl/Bash, Python, Java, C#/.NET code snippets using the ReqBin code generator.