Python HTTP Client Tutorial: GET, POST, PUT Requests with http.client
Learn how to use Python http.client to send HTTP and HTTPS requests, handle GET, POST, PUT methods, read responses, and fix SSL certificate errors.
Drake Nguyen
Founder · System Architect
The Python http.client module provides low-level tools for working with HTTP and HTTPS protocols on the client side. While it is often used internally by higher-level libraries such as urllib, understanding http.client helps developers gain full control over HTTP connections and request handling.

In this tutorial, you will learn how to create HTTP connections, send GET, POST, and PUT requests, inspect response status codes, read response bodies, and handle common SSL issues.
Python HTTP Client Overview
Using http.client, you can manually manage HTTP connections and perform REST-style requests. This approach is useful when you need minimal dependencies or want to understand how HTTP works at a lower level.
Making HTTP Connections
The simplest operation is creating a connection to a remote server. Below is an example of opening an HTTP connection with a custom timeout.
import http.client
connection = http.client.HTTPConnection(
'www.python.org',
80,
timeout=10
)
print(connection)
This example connects to the server on port 80 using the HTTP protocol. Always define a timeout to prevent your application from hanging indefinitely.
Sending a GET Request
To retrieve data from a server, you can send a GET request and inspect the response status and reason.
import http.client
conn = http.client.HTTPSConnection("www.journaldev.com")
conn.request("GET", "/")
response = conn.getresponse()
print("Status:", response.status)
print("Reason:", response.reason)
conn.close()
When working with secure websites, use HTTPSConnection instead of HTTPConnection. Always close the connection after completing the request.
Fixing SSL: CERTIFICATE_VERIFY_FAILED Error
On some systems, especially macOS, you may encounter the error: SSL: CERTIFICATE_VERIFY_FAILED.
This usually indicates missing or outdated local certificate authorities. On macOS, the issue can often be resolved by running the Install Certificates.command script included with Python.
On Linux distributions such as Ubuntu, this issue rarely occurs because system certificates are typically configured correctly by default.
Reading Response Headers
HTTP response headers contain valuable metadata such as content type, caching rules, and server information. You can retrieve them directly from the response object.
import http.client
import pprint
conn = http.client.HTTPSConnection("www.journaldev.com")
conn.request("GET", "/")
response = conn.getresponse()
headers = response.getheaders()
pprint.pprint(headers)
conn.close()
Sending a POST Request
POST requests are commonly used to send data to an API. The example below sends JSON data to an endpoint.
import http.client
import json
conn = http.client.HTTPSConnection("www.httpbin.org")
headers = {
"Content-Type": "application/json"
}
payload = {
"text": "Hello from Python HTTP client"
}
json_data = json.dumps(payload)
conn.request("POST", "/post", json_data, headers)
response = conn.getresponse()
print(response.read().decode())
conn.close()
Sending a PUT Request
PUT requests are typically used to update existing resources. The syntax is very similar to a POST request.
import http.client
import json
conn = http.client.HTTPSConnection("www.httpbin.org")
payload = {
"text": "Updated content"
}
json_data = json.dumps(payload)
conn.request("PUT", "/put", json_data)
response = conn.getresponse()
print(response.status, response.reason)
conn.close()
Conclusion
In this guide, you learned how to use Python’s http.client module to perform basic HTTP and HTTPS operations, including GET, POST, and PUT requests.
While higher-level libraries like requests are more convenient for most projects, understanding http.client gives you deeper insight into how HTTP communication works under the hood.