Skip to content

Instantly share code, notes, and snippets.

@Denilson-Semedo
Last active June 8, 2023 01:29
Show Gist options
  • Save Denilson-Semedo/1cfecd13fd124f8f419036bb1ad8fa1d to your computer and use it in GitHub Desktop.
Save Denilson-Semedo/1cfecd13fd124f8f419036bb1ad8fa1d to your computer and use it in GitHub Desktop.
Requests Library

Requests Library: Simplifying HTTP Requests in Python

The Requests library is a popular Python library for making HTTP requests and interacting with web services. It provides a simple and intuitive API that allows you to send HTTP requests, handle responses, manage cookies, handle authentication, and much more.

With the Requests library, you can easily perform common HTTP methods such as GET, POST, PUT, DELETE, HEAD, PATCH, and OPTIONS. It supports various features like passing parameters, adding headers, handling response content (including JSON), uploading files, managing cookies, and session management.

The library is widely used due to its user-friendly design, robustness, and extensive documentation. It abstracts the complexities of working with HTTP and provides a high-level interface that makes it straightforward to integrate web services into your Python applications.

Python Requests Documentation

Method Description Example
GET Sends an HTTP GET request to the specified URL and retrieves the response. import requests
response = requests.get('https://api.example.com/data')
POST Sends an HTTP POST request to the specified URL with optional data. payload = {'key1': 'value1', 'key2': 'value2'}
response = requests.post('https://api.example.com/data', data=payload)
PUT Sends an HTTP PUT request to the specified URL with optional data. payload = {'key1': 'value1', 'key2': 'value2'}
response = requests.put('https://api.example.com/data', data=payload)
DELETE Sends an HTTP DELETE request to the specified URL. response = requests.delete('https://api.example.com/data')
HEAD Sends an HTTP HEAD request to the specified URL. response = requests.head('https://api.example.com/data')
PATCH Sends an HTTP PATCH request to the specified URL with optional data. payload = {'key1': 'value1', 'key2': 'value2'}
response = requests.patch('https://api.example.com/data', data=payload)
OPTIONS Sends an HTTP OPTIONS request to the specified URL. response = requests.options('https://api.example.com/data')
Sending Parameters Includes parameters in the URL for a GET request. payload = {'key1': 'value1', 'key2': 'value2'}
response = requests.get('https://api.example.com/data', params=payload)
Sending Headers Adds custom headers to the request. headers = {'Content-Type': 'application/json'}
response = requests.get('https://api.example.com/data', headers=headers)
Handling Response Retrieves response content and status code. response = requests.get('https://api.example.com/data')
content = response.content
status_code = response.status_code
Handling JSON Response Parses JSON response into a Python object. response = requests.get('https://api.example.com/data')
data = response.json()
Uploading Files Sends a POST request with a file attachment. files = {'file': open('file.txt', 'rb')}
response = requests.post('https://api.example.com/upload', files=files)
Managing Cookies Manages cookies in requests. response = requests.get('https://api.example.com/data')
cookies = response.cookies
cookie_value = cookies['cookie_name']
Session Management Maintains cookies across multiple requests using sessions. session = requests.Session()
response = session.get('https://api.example.com/login')
response = session.get('https://api.example.com/data')
SSL Verification Verifies SSL certificates for secure connections. response = requests.get('https://api.example.com/data', verify=True)
Timeout Sets a timeout for the request. response = requests.get('https://api.example.com/data', timeout=5)
Error Handling Handles request errors and exceptions. try:
response = requests.get('https://api.example.com/data')
response.raise_for_status()
except requests.exceptions.HTTPError as err:
print(f"HTTP Error occurred: {err}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment