Last Updated : 31 Jul, 2025
Python Requests Library is a simple and powerful tool to send HTTP requests and interact with web resources. It allows you to easily send GET, POST, PUT, DELETE, PATCH, HEAD requests to web servers, handle responses, and work with REST APIs and web scraping tasks.
Why do we need Requests LibraryTo install requests library via pip, use the following command:
Syntaxpip install requests
requests.get(url, params={key: value}, **kwargs)
Parameter:
Return Type: It returns a response object.
Making a Simple GET RequestLet's try making a get request to URL: "https://www.geeksforgeeks.org/".
Python
import requests
response = requests.get("https://www.geeksforgeeks.org/")
print(response.status_code)
Output
200
Explanation:
Let's demonstrate how to make a GET request to an endpoint. The GET method sends encoded user information appended to the page request.
Example: Let's try making a request to github's APIs for example purposes.
Python
import requests
response = requests.get("https://api.github.com/users/naveenkrnl")
print(response.status_code)
print(response.content)
Output
Output of Sending GET request with parameterHTTP Request Methods Method Description GET Retrieve information from the server POST Send data to the server to create/update resources PUT Replace the target resource with new data. DELETE The DELETE method deletes the specified resource HEAD Retrieve headers only (no body) PATCH Apply partial modifications to a resource Response objectFor more, visit: GET method β Python requests
Whenever you send a request, the server returns a Response object. It contains useful information like status code, headers, content, and more. Response object can be used to imply lots of features, methods, and functionalities.
Example:
Python
import requests
response = requests.get('https://api.github.com/')
print(response.url)
print(response.status_code)
Output
Output of Response objectExplanation:
import requests
payload = {'username': 'test', 'password': 'test123'}
response = requests.post("https://httpbin.org/post", data=payload)
print(response.text)
Output
Output of POST requestExplanation:
Authentication refers to giving a user permissions to access a particular resource. Since, everyone can't be allowed to access data from every URL, one would require authentication primarily. To achieve this authentication, typically one provides authentication data through Authorization header or a custom header defined by server.
Requests module makes this simple using HTTPBasicAuth.
Example:
Python
import requests
from requests.auth import HTTPBasicAuth
response = requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))
print(response.status_code)
Output
401
Explanation:
SSL Certificate VerificationFor more visit - Authentication using Python requests
Requests verifies SSL certificates for HTTPS requests, just like a web browser. SSL Certificates are small data files that digitally bind a cryptographic key to an organization's details. Often, an website with a SSL certificate is termed as secure website. By default, SSL verification is enabled, and Requests will throw a SSLError if itβs unable to verify the certificate.
Accessing a site with invalid SSL:Let us try to access a website with an invalid SSL certificate, using Python requests
Python
import requests
response = requests.get('https://expired.badssl.com/', verify=False)
print(response.status_code)
Output
Output of Accessing a site with valid SSLExplanation: Setting verify=False disables SSL verification (not recommended for production).
Providing a custom certificate:The above website doesn't have SSL setup so it raises this error, one can also pass the link to the certificate for validation via python requests only.
Python
import requests
response = requests.get('https://github.com/', verify='/path/to/certfile')
print(response.status_code)
This would work in case the path provided is correct for SSL certificate for github.com.
Session ObjectsFor more visit- SSL Certificate Verification β Python requests
Session objects allow you to persist settings across multiple requests, such as headers, cookies, and connection pooling. So if several requests are being made to the same host, the underlying TCP connection will be reused, which can result in a significant performance increase. A session object all the methods as of requests.
Using Session ObjectsLet us illustrate use of session objects by setting a cookie to a url and then making a request again to check if cookie is set.
Python
import requests
# Create a session object
session = requests.Session()
# Set a cookie
session.get('https://httpbin.org/cookies')
# Access the cookie in the next request
response = session.get('https://httpbin.org/cookies')
print(response.text)
Output
Output of Session objectsExplanation:
Error Handling with RequestsFor more, visit - Session Objects β Python requests
If this code doesn't print anything, it means request was successful and no errors occurred during the process.
Python
import requests
try:
response = requests.get("https://www.example.com/", timeout=5)
response.raise_for_status()
except requests.exceptions.HTTPError as errh:
print("HTTP Error:", errh)
except requests.exceptions.ConnectionError as errc:
print("Connection Error:", errc)
except requests.exceptions.Timeout as errt:
print("Timeout Error:", errt)
except requests.exceptions.RequestException as err:
print("Something Else:", err)
Explanation:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4