How to force Curl to close the connection after response? [Curl/Bash Code]
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. The Curl/Bash code was automatically generated for the Curl Close Connection Header example.
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.
The table shows the difference between closing connections in HTTP versions.
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.
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.
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.