
![]() |
@wtf | |
Requests HTTP Library |
||
1
Replies
21
Views
1 Bookmarks
|
![]() |
@wtf | 8 August 25 |
The requests library is the go-to tool in Python for making HTTP requests, used heavily in web scr*ping, APIs, and automation tasks involving the web. Why Use Requests? Simplifies sending HTTP/HTTPS requests Works great with REST APIs Handles GET, POST, PUT, DELETE, etc. Supports headers, parameters, timeouts, and sessions Easy to handle JSON responses Common Use Cases 1. GET Request python import requests response = requests.get('https://api.example.com/data') print(response.status_code) print(response.json()) 2. POST Request with Data python data = 'name': 'John', 'age': 30 response = requests.post('https://api.example.com/submit', data=data) 3. Passing Headers python headers = 'Authorization': 'Bearer YOUR_TOKEN' response = requests.get('https://api.example.com/protected', headers=headers) 4. Handling JSON Data python response = requests.get('https://api.example.com/data') data = response.json() print(data['key']) 5. Error Handling & Timeout python try: response = requests.get('https://api.example.com', timeout=5) response.raise_for_status() except requests.exceptions.RequestException as e: print(e) Common Methods - requests.get() - requests.post() - requests.put() - requests.delete() - .json() to parse JSON responses - .status_code, .headers, .text to inspect responses Real-World Use Cases Fetching API data Web automation/scr*ping Bots and alerts Building dashboards or data pipelines Summary Ideal For: Accessing & working with APIs Strength: Simplicity + flexibility Bonus: Helps automate anything that talks over the web |
||


