![]() |
VOOZH | about |
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.
To install requests library via pip, use the following command:
pip install requests
requests.get(url, params={key: value}, **kwargs)
Parameter:
Return Type: It returns a response object.
Let's try making a get request to URL: "https://www.geeksforgeeks.org/".
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.
Output
For more, visit: GET method β Python requests
| 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 |
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:
Output
Explanation:
| Method | Description |
|---|---|
| response.headers | response.headers returns a dictionary of response headers. |
| response.encoding | response.encoding returns the encoding used to decode response.content. |
| response.elapsed | response.elapsed returns a timedelta object with the time elapsed from sending the request to the arrival of the response. |
| response.close() | response.close() closes the connection to the server. |
| response.content | response.content returns the content of the response, in bytes. |
| response.cookies | response.cookies returns a CookieJar object with the cookies sent back from the server. |
| response.history | response.history returns a list of response objects holding the history of request (url). |
| response.is_permanent_redirect | response.is_permanent_redirect returns True if the response is the permanent redirected url, otherwise False. |
| response.is_redirect | response.is_redirect returns True if the response was redirected, otherwise False. |
| response.iter_content() | response.iter_content() iterates over the response.content. |
| response.json() | response.json() returns a JSON object of the result (if the result was written in JSON format, if not it raises an error). |
| response.url | response.url returns the URL of the response. |
| response.text | response.text returns the content of the response, in unicode. |
| response.status_code | response.status_code returns a number that indicates the status (200 is OK, 404 is Not Found). |
| response.request | response.request returns the request object that requested this response. |
| response.reason | response.reason returns a text corresponding to the status code. |
| response.raise_for_status() | response.raise_for_status() returns an HTTPError object if an error has occurred during the process. |
| response.ok | response.ok returns True if the status code is less than 400 and greater than or equal to 200; otherwise, it returns False |
| response.links | response.links returns the header links. |
Output
Explanation:
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:
Output
401
Explanation:
For 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.
Let us try to access a website with an invalid SSL certificate, using Python requests
Output
Explanation: Setting verify=False disables SSL verification (not recommended for production).
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.
This would work in case the path provided is correct for SSL certificate for github.com.
For 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.
Let 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.
Output
Explanation:
For 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.
Explanation: