Sending Curl Request with Close Connection Header [Python Code]

Curl tries to keep connections open and reuse existing connections. To close a connection after a request, you can send a "Connection: close" HTTP header to the server. The Connection: close header informs the server that the client wants to close the connection after completing the HTTP transaction. It's up to the server to close the connection after the request or not. You can use the- H command-line option to pass the "Connection: close" header to Curl. In this Curl Close Connection example, we send a request to the ReqBin echo URL with the corresponding header. Click Run to execute the Сurl Сommand with Сlose Сonnection Header example online and see the result. The Python code was automatically generated for the Curl Close Connection Header example.
Sending Curl Request with Close Connection Header [Python Code] Run
curl https://reqbin.com/echo
     -H "Connection: close"
Updated: Viewed: 38720 times
Python code for Curl Close Connection Header example

Python code for Curl Close Connection Header Example

This Python code snippet was generated automatically for the Curl Close Connection Header example.
<< Back to the Curl Close Connection Header example

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.

Curl Close Connection Header Syntax

The following is the syntax of sending a Curl request with a Close Connection Header:

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
curl https://reqbin.com/echo 
     -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 (Transmission Control Protocol) is a communication protocol that allows computing devices and application programs to exchange messages over a network. TCP is one of the main Internet standards defined by the IETF (Internet Engineering Task Force), which defines the rules of the Internet. TCP is one of the most commonly used protocols in digital network communications and provides end-to-end data delivery.

See also

Generate code snippets for Python and other programming languages

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