How to force Curl to close the connection after response?

Close Сonnection means that the client or server wants to close the connection after finishing the HTTP transaction. The Curl client can tell the server that it wants to close the connection by sending the "Connection: close" header to the server. To pass the "Connection: close" header to Curl, you can use the -H command-line option. In this Curl Close Connection example, we are sending a request to the ReqBin echo URL. Click Run to execute the Curl example online and see the results.
How to force Curl to close the connection after response? Run
   -H "Connection: close"
Updated: Viewed: 14923 times

What is Curl?

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

What is Connection Header?

The Сonnection Header determines whether the current network connection remains open after a transaction. A persistent connection allows multiple requests to be sent without opening a new connection for each request/response pair. When the "Connection: close" header is used in the request message, the server closes the connection after sending the response message. To pass the "Connection: close" header for the Curl request, use the -H command-line option.

Close Connection Header Syntax
curl -H "Connection: close" [URL]

How to Close a Connection in Curl?

This example shows how to tell Curl to close the connection by passing in the "Connection: close" header using the -H command line parameter.

Close Connection Header using Curl
   -H "Connection: close"

Close Connection in HTTP versions

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

HTTP version Description
HTTP/1.0 In HTTP/1.0, the server by default always closes the connection after sending a response, unless the client has sent a "Connection: open" request header. If there is no such header, the server will close the connection after sending the response to the client.
HTTP/1.1 In HTTP 1.1, the server does not close the connection after sending the response, and clients need to pass the "Connection: close" header if they want the connection to be closed after receiving the response from the server. An HTTP/1.1 server typically supports 2 to 8 open TCP connections per client.
HTTP/2 In HTTP/2, requests are bundled into the same TCP connection so that the browser can send them simultaneously without waiting for a response to a previous request. On the server-side, connections from various clients will remain open until they are closed by the client or the server-side timed out. In HTTP/2, the server only needs to support one TCP connection per client, which is less than in HTTP/1.1

What is TCP connection?

TCP (stands for Transmission Control Protocol) is a communication protocol that allows the establishment and maintenance of a network conversation through which devices can exchange data. TCP works with the Internet Protocol (IP), which determines how computers send data packets to each other. Together, TCP and IP are the basic rules governing the Internet.

See also

Generate Code Snippets for Curl Close Connection Header Example

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