How to Make HTTP Request in Python: Simple Guide
To make an
HTTP request in Python, use the requests library by calling methods like requests.get() or requests.post(). This library handles sending the request and receiving the response easily with simple code.Syntax
The basic syntax to make an HTTP GET request is using requests.get(url). You provide the URL as a string. For POST requests, use requests.post(url, data=data_dict) where data_dict is the data you want to send.
The response object returned contains the server's reply, accessible via response.text for content or response.status_code for status.
python
import requests response = requests.get('https://example.com') print(response.status_code) print(response.text[:100]) # print first 100 characters
Output
200
<!doctype html>
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
body {
background-color: #f0f0f2;
margin: 0;
padding: 0;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
</style>
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents.</p>
</div>
</body>
</html>
Example
This example shows how to send a GET request to a website and print the status code and first 200 characters of the response content.
python
import requests url = 'https://httpbin.org/get' response = requests.get(url) print('Status code:', response.status_code) print('Response content:', response.text[:200])
Output
Status code: 200
Response content: {
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.31.0",
"X-Amzn-Trace-Id": "Root=1-64b7a7a0-4a7e3e9a2a7e3e9a2a7e3e9a"
},
"origin": "0.0.0.0",
"url": "https://httpbin.org/get"
}
Common Pitfalls
- Not installing the
requestslibrary before use (install withpip install requests). - Forgetting to check the
response.status_codeto ensure the request succeeded. - Trying to use
requests.get()with data payloads (userequests.post()for sending data). - Not handling exceptions like connection errors which can crash the program.
python
import requests # Wrong: sending data with GET response = requests.get('https://httpbin.org/get', params={'key': 'value'}) print('Status code:', response.status_code) # Right: sending data with POST response = requests.post('https://httpbin.org/post', data={'key': 'value'}) print('Status code:', response.status_code)
Output
200
200
Quick Reference
| Method | Description | Example |
|---|---|---|
| requests.get(url) | Send a GET request to retrieve data | requests.get('https://example.com') |
| requests.post(url, data) | Send a POST request with data | requests.post('https://example.com', data={'key':'value'}) |
| response.status_code | Get HTTP status code of response | print(response.status_code) |
| response.text | Get response content as text | print(response.text) |
| requests.exceptions.RequestException | Base exception for request errors | try: ... except requests.exceptions.RequestException: |
Key Takeaways
Use the requests library to easily make HTTP requests in Python.
Always check response.status_code to confirm success.
Use requests.get() for retrieving data and requests.post() for sending data.
Handle exceptions to avoid crashes on network errors.
Install requests with pip before using it.