Skip to main content

Requests Module in Python

 


Learning Sections          show

Requests Module in Python

The requests module in Python is a popular library used to make HTTP requests in a simple and human-friendly way. It abstracts the complexities of making requests behind a simple API, allowing you to send HTTP requests with minimal effort.


Installing Requests

You can install the requests module using pip:


$ pip install requests
    

Making a Simple GET Request

The most common use of the requests module is to send a GET request to retrieve data from a server:


import requests as r

# Make a GET request
response = r.get('https://api.github.com')
# Print the response text
print(response.text)
    

The get function sends a GET request to the specified URL and returns a response object, which you can use to access various elements of the response.


Handling Response Content

The response object contains all the data returned by the server, and you can access different parts of it:


# Print response status code
print(response.status_code)

# Print response headers
print(response.headers)

# Print response JSON content
print(response.json())
    

You can access the response's status code, headers, and content in various formats such as text, JSON, or binary.


Making a POST Request

You can send data to the server using the POST method:


# Define the data to send in the POST request
data = {
    'key1': 'value1',
    'key2': 'value2'
}

# Make a POST request
response = r.post('https://httpbin.org/post', data=data)

# Print the response text
print(response.text)
    

The post function sends a POST request with the specified data to the server.


Handling URL Parameters

You can pass parameters in the URL of a GET request using the params keyword:


# Define the URL parameters
params = {
    'param1': 'value1',
    'param2': 'value2'
}

# Make a GET request with URL parameters
response = r.get('https://httpbin.org/get', params=params)

# Print the response text
print(response.text)
    

The params keyword allows you to include URL parameters in your GET request.


Uploading Files

You can upload files using the files keyword:


# Define the file to upload
files = {
    'file': ('filename.txt', open('filename.txt', 'rb'))
}

# Make a POST request to upload the file
response = r.post('https://httpbin.org/post', files=files)

# Print the response text
print(response.text)
    

Handling Authentication

The requests module supports different types of authentication, such as Basic Authentication:


from requests.auth import HTTPBasicAuth

# Make a GET request with Basic Authentication
response = r.get(
    'https://api.github.com/user',
    auth=HTTPBasicAuth('username', 'password')
)

# Print the response text
print(response.text)
    

The auth keyword allows you to include authentication details in your request.


Session Objects

Session objects allow you to persist parameters across multiple requests:


# Create a session object
session = r.Session()

# Define the URL parameters
session.params = {
    'param1': 'value1',
    'param2': 'value2'
}

# Make a GET request with the session
response = session.get('https://httpbin.org/get')

# Print the response text
print(response.text)
    

Session objects can be used to persist certain parameters, such as cookies and headers, across multiple requests.


Custom Headers

You can send custom headers in your requests using the headers keyword:


# Define the custom headers
headers = {
    'User-Agent': 'my-app/0.0.1'
}

# Make a GET request with custom headers
response = r.get('https://api.github.com', headers=headers)

# Print the response text
print(response.text)
    

The headers keyword allows you to send custom headers with your requests.


Timeouts

You can specify a timeout for your requests using the timeout keyword:


# Make a GET request with a timeout
response = r.get('https://api.github.com', timeout=5)

# Print the response text
print(response.text)
    

The timeout keyword specifies the maximum number of seconds to wait for a response.

Popular posts from this blog

Introduction to Python Programming

  Learning Sections      show History of Python Python was created by Guido van Rossum and first released in 1991. He wanted to create a language that was easy to read and simple to use. The name "Python" comes from the British comedy series "Monty Python's Flying Circus". Key Features of Python Readability: Python's syntax is clear and easy to read. Ease of Learning: Python is straightforward, making it great for beginners. High-Level Language: Python handles much of the complexity of the computer’s operations. Interpreted Language: Python runs code line-by-line, which makes debugging easier. Dynamically Typed: You don’t need to declare variable types. Extensive Standard Library: Python has many built-in modules for various tasks. Portability: ...

Learn Python

  Learning Sections Introduction to Python Comment, escape sequence and print statement in Python Variables and Data Types in Python Typecasting in Python User input in Python String slicing and operations on string in Python String methods in Python If else conditional statements in Python Match case statement in Python For loops in Python While loops in Python Break and continue statement in Python Functions in Python Function Arguments in Python introduction to lists in Python List methods in Python Tuples in Python Operations on tuple in Python f strings in Python Docstrings in Python Recursion in Python Sets in Python Set methods in Python Dictionaries in Python for Loop with else in Python Exception Handling in Python Finally keyword in Python Raising custom errors in Python Short hand if else statements Enumerate Function in Python Virtual Environment in Python How import works in Python if __nam...

Comment, escape sequence and print statement in Python

Learning Sections      show 1. Comments in Python Comments are notes in the code that the Python interpreter ignores. They are used to explain and document the code, making it easier to understand and maintain. Single-line comments: Begin with the # symbol. Multi-line comments: Typically use triple quotes ''' or """ . # Single-line comment print ( "Hello, World!" ) # This comment is on the same line as the code """ Multi-line comment: This spans multiple lines. The Python interpreter will ignore these lines. """ print ( "Multi-line comments are often used for documentation." ) 2. Escape Sequences in Python Escape sequences are used to insert special characters into strings that are otherwise difficult to include directly. An escape sequence begins with a backslash ( \ ) followed by one or more characters. # Using escape s...