Language: Python
Web
Requests was created by Kenneth Reitz in 2011 to simplify HTTP requests in Python. Its goal was to provide a more readable and intuitive API compared to urllib and urllib2. Over the years, it became one of the most widely used Python libraries for web interactions, API clients, and automation tasks.
Requests is an elegant and simple HTTP library for Python, designed to make sending HTTP/1.1 requests easy and human-friendly. It abstracts the complexities of handling HTTP connections, cookies, headers, and authentication.
pip install requestsconda install requestsRequests allows you to send HTTP/1.1 requests using methods such as GET, POST, PUT, DELETE, HEAD, and OPTIONS. You can pass parameters, headers, JSON payloads, handle cookies, manage sessions, and handle authentication easily.
import requests
response = requests.get('https://httpbin.org/get')
print(response.status_code)
print(response.json())This example performs a GET request to a URL and prints the HTTP status code and JSON response.
import requests
payload = {'key':'value'}
response = requests.post('https://httpbin.org/post', json=payload)
print(response.json())Sends a POST request with a JSON payload to the specified URL.
import requests
session = requests.Session()
session.get('https://httpbin.org/cookies/set/sessioncookie/123456789')
response = session.get('https://httpbin.org/cookies')
print(response.text)Uses a Session object to persist cookies and headers across multiple requests.
import requests
headers = {'User-Agent': 'my-app/0.0.1'}
response = requests.get('https://httpbin.org/headers', headers=headers)
print(response.json())Shows how to send custom HTTP headers with a request.
import requests
try:
response = requests.get('https://httpbin.org/delay/10', timeout=5)
except requests.exceptions.Timeout:
print('Request timed out')Demonstrates handling of request timeouts.