Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
293 changes: 257 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Requests

**Requests** is a simple, yet elegant, HTTP library.
**Requests** is a simple, yet elegant, HTTP library for Python, making HTTP requests incredibly straightforward.

```python
>>> import requests
Expand All @@ -17,62 +17,283 @@
{'authenticated': True, ...}
```

Requests allows you to send HTTP/1.1 requests extremely easily. There’s no need to manually add query strings to your URLs, or to form-encode your `PUT` & `POST` data — but nowadays, just use the `json` method!
---

## 🚀 Quick Start

### Installation
Requests is available on PyPI and can be installed via pip:

```bash
pip install requests
```

Supported Python versions: 3.9+

### Verify Installation

```python
import requests
print(f"Requests version: {requests.__version__}")
```

---

## 💡 Common Usage Examples

Requests is one of the most downloaded Python packages today, pulling in around `30M downloads / week`— according to GitHub, Requests is currently [depended upon](https://github.com/psf/requests/network/dependents?package_id=UGFja2FnZS01NzA4OTExNg%3D%3D) by `1,000,000+` repositories. You may certainly put your trust in this code.
### Basic GET Request

[![Downloads](https://static.pepy.tech/badge/requests/month)](https://pepy.tech/project/requests)
[![Supported Versions](https://img.shields.io/pypi/pyversions/requests.svg)](https://pypi.org/project/requests)
[![Contributors](https://img.shields.io/github/contributors/psf/requests.svg)](https://github.com/psf/requests/graphs/contributors)
```python
import requests

# Simple GET request
response = requests.get('https://api.github.com')
print(f"Status Code: {response.status_code}")
print(f"Headers: {response.headers['content-type']}")

## Installing Requests and Supported Versions
# Access JSON data
data = response.json()
print(f"GitHub API Rate Limit: {data['resources']['core']['limit']}")
```

Requests is available on PyPI:
### POST Request with JSON Data

```console
$ python -m pip install requests
```python
import requests

# POST with JSON data
payload = {'name': 'John', 'email': '[email protected]'}
response = requests.post('https://httpbin.org/post', json=payload)

print(f"Status: {response.status_code}")
print(f"Response: {response.json()}")
```

Requests officially supports Python 3.9+.
### Handling Authentication

## Supported Features & Best–Practices
```python
import requests
from requests.auth import HTTPBasicAuth

Requests is ready for the demands of building robust and reliable HTTP–speaking applications, for the needs of today.
# Basic Authentication
response = requests.get(
'https://httpbin.org/basic-auth/user/pass',
auth=HTTPBasicAuth('user', 'pass')
)

- Keep-Alive & Connection Pooling
- International Domains and URLs
- Sessions with Cookie Persistence
- Browser-style TLS/SSL Verification
- Basic & Digest Authentication
- Familiar `dict`–like Cookies
- Automatic Content Decompression and Decoding
- Multi-part File Uploads
- SOCKS Proxy Support
- Connection Timeouts
- Streaming Downloads
- Automatic honoring of `.netrc`
- Chunked HTTP Requests
# Or simpler:
response = requests.get(
'https://httpbin.org/basic-auth/user/pass',
auth=('user', 'pass')
)
```

### Working with Query Parameters

```python
import requests

## API Reference and User Guide available on [Read the Docs](https://requests.readthedocs.io)
# Automatic query parameter handling
params = {'search': 'python', 'page': 1, 'limit': 10}
response = requests.get('https://httpbin.org/get', params=params)

[![Read the Docs](https://raw.githubusercontent.com/psf/requests/main/ext/ss.png)](https://requests.readthedocs.io)
print(f"URL: {response.url}")
print(f"Params: {response.json()['args']}")
```

## Cloning the repository
---

When cloning the Requests repository, you may need to add the `-c
fetch.fsck.badTimezone=ignore` flag to avoid an error about a bad commit timestamp (see
[this issue](https://github.com/psf/requests/issues/2690) for more background):
## 🛠️ Advanced Features

### Session Management

```python
import requests

# Use sessions for connection pooling and cookie persistence
with requests.Session() as session:
session.headers.update({'User-Agent': 'MyApp/1.0'})

# All requests in this session share cookies and connections
response1 = session.get('https://httpbin.org/cookies/set/sessioncookie/123456789')
response2 = session.get('https://httpbin.org/cookies')

print(f"Cookies: {response2.json()}")
```

### File Upload

```python
import requests

# Upload a file
files = {'file': open('report.pdf', 'rb')}
response = requests.post('https://httpbin.org/post', files=files)

# Upload with additional data
files = {'file': ('report.pdf', open('report.pdf', 'rb'), 'application/pdf')}
data = {'description': 'Quarterly report'}
response = requests.post('https://httpbin.org/post', files=files, data=data)
```

### Streaming Large Responses

```python
import requests

# Stream large responses to avoid loading everything into memory
response = requests.get('https://httpbin.org/stream/100', stream=True)

for line in response.iter_lines():
if line:
print(f"Received: {line.decode('utf-8')}")
```

---

## 🐛 Troubleshooting Common Issues

### Handling Errors Gracefully

```python
import requests
from requests.exceptions import RequestException

try:
response = requests.get('https://httpbin.org/status/404', timeout=5)
response.raise_for_status() # Raises an HTTPError for bad responses
except requests.exceptions.HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
except requests.exceptions.ConnectionError as conn_err:
print(f'Connection error occurred: {conn_err}')
except requests.exceptions.Timeout as timeout_err:
print(f'Timeout error occurred: {timeout_err}')
except requests.exceptions.RequestException as req_err:
print(f'An error occurred: {req_err}')
```

```shell
### SSL Certificate Verification

```python
import requests

# For self-signed certificates (development only)
response = requests.get('https://self-signed-example.com', verify=False)

# Or specify a custom CA bundle
response = requests.get('https://example.com', verify='/path/to/certfile.pem')
```

### Setting Timeouts

```python
import requests

# Always set timeouts to avoid hanging requests
try:
response = requests.get('https://httpbin.org/delay/10', timeout=3.5)
except requests.exceptions.Timeout:
print("Request timed out!")
```

---

## 📊 Supported Features

Requests is ready for building robust HTTP applications with:

✅ Keep-Alive & Connection Pooling - Efficient connection reuse
✅ International Domains and URLs - Full Unicode support
✅ Sessions with Cookie Persistence - Maintain state across requests
✅ Browser-style TLS/SSL Verification - Secure by default
✅ Basic & Digest Authentication - Multiple auth methods
✅ Automatic Content Decompression - gzip, deflate compression
✅ Streaming Downloads - Handle large responses efficiently
✅ SOCKS Proxy Support - Complete proxy support
✅ Connection Timeouts - Prevent hanging requests
✅ Chunked HTTP Requests - Efficient large uploads

---

## 🔧 Installation Options

### Using conda

```bash
conda install -c anaconda requests
```

### From Source

```bash
git clone https://github.com/psf/requests.git
cd requests
pip install -e .
```

### Development Installation

```bash
git clone https://github.com/psf/requests.git
cd requests
pip install -e ".[socks]" # With optional SOCKS support
pip install -r requirements-dev.txt # Development dependencies
```

---

## 📚 Documentation

Complete API reference and user guide available on [Read the Docs](https://requests.readthedocs.io).

![Requests Example](https://raw.githubusercontent.com/psf/requests/main/ext/ss.png)

---

## 🤝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

### Cloning the Repository

```bash
git clone -c fetch.fsck.badTimezone=ignore https://github.com/psf/requests.git
```

You can also apply this setting to your global Git config:
Or set globally:

```shell
```bash
git config --global fetch.fsck.badTimezone ignore
```

---

[![Kenneth Reitz](https://raw.githubusercontent.com/psf/requests/main/ext/kr.png)](https://kennethreitz.org) [![Python Software Foundation](https://raw.githubusercontent.com/psf/requests/main/ext/psf.png)](https://www.python.org/psf)
## 📈 Project Stats

![Downloads](https://static.pepy.tech/badge/requests/month)
![Python Versions](https://img.shields.io/pypi/pyversions/requests.svg)
![Contributors](https://img.shields.io/github/contributors/psf/requests.svg)

Requests is one of the most downloaded Python packages with over **30 million downloads per week** and trusted by **1,000,000+ repositories**.

![PSF](https://raw.githubusercontent.com/psf/requests/main/ext/psf.png)

---

## 🛠️ EXACT STEPS TO APPLY

1. Open `README.md`
2. Select ALL (Ctrl+A) and DELETE everything
3. COPY this improved version and PASTE it into the file
4. SAVE (Ctrl+S)

---

## 🚀 THEN COMMIT & PUSH

```bash
git checkout -b docs/improve-readme-examples
git add README.md
git commit -m "docs: completely rewrite README with comprehensive examples and troubleshooting guide"
git push origin docs/improve-readme-examples
```