From cf0ec4d920c654da45834a6aea53701affa37f6a Mon Sep 17 00:00:00 2001 From: Your Actual Name Date: Fri, 31 Oct 2025 18:46:04 +0530 Subject: [PATCH] docs: completely rewrite README with comprehensive examples and troubleshooting guide --- README.md | 293 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 257 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 74adab80dd..e754b083d3 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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': 'john@example.com'} +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 +```