|
1 |
| - #Request Module in Python |
2 |
| - >>Introduction |
3 |
| - The Requests library in Python is one of the integral parts of Python for making HTTP requests to a specified URL. Whether it be REST APIs |
4 |
| - or Web Scraping, requests are a must to be learned for proceeding further with these technologies. When one makes a request to a URI, it |
5 |
| - returns a response. Python requests provide inbuilt functionalities for managing both the request and response |
6 |
| - |
7 |
| - >> What is Python Requests module? |
8 |
| - Requests is an Apache2 Licensed HTTP library, that allows to send HTTP/1.1 requests using Python. |
9 |
| - To play with web, Python Requests is must. Whether it be hitting APIs, downloading entire facebook pages, and much more cool stuff, one will |
10 |
| - have to make a request to the URL. |
11 |
| - |
12 |
| - * Installing Requests |
13 |
| - Requests installation depends on type of operating system on eis using, the basic command anywhere would be to open a command terminal and run, |
14 |
| - |
15 |
| - pip install requests |
16 |
| - |
17 |
| - * Making a Request |
18 |
| - Python requests module has several built-in methods to make Http requests to specified URI using GET, POST, PUT, PATCH or HEAD requests. A |
19 |
| - Http request is meant to either retrieve data from a specified URI or to push data to a server. It works as a request-response protocol |
20 |
| - between a client and a server. Let’s demonstrate how to make a GET request to an endpoint. GET method is used to retrieve information from the |
21 |
| - given server using a given URI. The GET method sends the encoded user information appended to the page request. The page and the encoded |
22 |
| - information are separated by the ‘?’ character. For example: https://www.google.com/search?q=... |
23 |
| - |
24 |
| - >>How to make GET request through Python Requests |
25 |
| - Python’s requests module provides in-built method called get() for making a GET request to a specified URI. |
26 |
| - |
27 |
| - * Syntax |
28 |
| - requests.get(url, params={key: value}, args) |
29 |
| - |
30 |
| - Example : |
31 |
| - Let’s try making a request to github’s APIs for example purposes. |
32 |
| - import requests |
33 |
| - |
34 |
| - # Making a GET request |
35 |
| - r = requests.get('https://api.github.com/...') |
36 |
| - |
37 |
| - # check status code for response received |
38 |
| - # success code - 200 |
39 |
| - print(r) |
40 |
| - |
41 |
| -# print content of request |
42 |
| - print(r.content) |
43 |
| - save this file as request.py and through terminal run, |
44 |
| - python request.py |
45 |
| - |
46 |
| - |
47 |
| - >> Http Request Methods |
48 |
| - Method Description |
49 |
| - 1. GET GET method is used to retrieve information from the given server using a given URI. |
50 |
| - 2.POST POST request method requests that a web server accepts the data enclosed in the body of the request message, most likely for |
51 |
| - storing |
52 |
| - 3. PUT The PUT method requests that the enclosed entity be stored under the supplied URI. If the URI refers to an already existing |
53 |
| - resource, it is modified and if the URI does not point to an existing resource, then the server can create the resource with that |
54 |
| - URI. |
55 |
| - 4.DELETE The DELETE method deletes the specified resource |
56 |
| - 5.HEAD The HEAD method asks for a response identical to that of a GET request, but without the response body. |
57 |
| - 6.PATCH It is used for modify capabilities. The PATCH request only needs to contain the changes to the resource, not the complete |
58 |
| - resource |
59 |
| - |
60 |
| - |
61 |
| - >> Response object |
62 |
| - When one makes a request to a URI, it returns a response. This Response object in terms of python is returned by requests.method(), method |
63 |
| - being – get, post, put, etc. Response is a powerful object with lots of functions and attributes that assist in normalizing data or creating |
64 |
| - ideal portions of code. For example, response.status_code returns the status code from the headers itself, and one can check if the request |
65 |
| - was processed successfully or not. Response object can be used to imply lots of features, methods, and functionalities. |
66 |
| - |
67 |
| - # import requests module |
68 |
| - import requests |
69 |
| - # Making a get request |
70 |
| - response = requests.get('https://api.github.com/') |
71 |
| - # print request object |
72 |
| - print(response.url) |
73 |
| - # print status code |
74 |
| - print(response.status_code) |
75 |
| - |
76 |
| - >> Response Methods |
77 |
| - Method ` ` Description |
78 |
| - 1.response.headers response.headers returns a dictionary of response headers. |
79 |
| - 2.response.encoding response.encoding returns the encoding used to decode response.content. |
80 |
| - 3.response.elapsed response.elapsed returns a timedelta object with the time elapsed from sending the request to the arrival of the response. |
81 |
| - 4.response.close() response.close() closes the connection to the server. |
82 |
| - 5.response.content response.content returns the content of the response, in bytes. |
83 |
| - 6.response.cookies response.cookies returns a CookieJar object with the cookies sent back from the server. |
84 |
| - 7.response.history response.history returns a list of response objects holding the history of request (url). |
85 |
| - 8.response.is_permanent_redirect returns True if the response is the permanent redirected url, otherwise False. |
| 1 | +# Python Requests Module Project |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +This project demonstrates how to use the Python `requests` module to make HTTP requests. It includes examples of GET, POST, PUT, DELETE, HEAD, and PATCH requests, as well as handling responses and extracting useful information. |
| 6 | + |
| 7 | +## Table of Contents |
| 8 | + |
| 9 | +- [Introduction](#introduction) |
| 10 | +- [Installation](#installation) |
| 11 | +- [Usage](#usage) |
| 12 | + - [Making a GET Request](#making-a-get-request) |
| 13 | + - [Other HTTP Methods](#other-http-methods) |
| 14 | + - [Response Object](#response-object) |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +## Introduction |
| 19 | + |
| 20 | +The `requests` library in Python is a simple and elegant HTTP library. It allows you to send HTTP/1.1 requests with methods like GET and POST, add headers, form data, multipart files, and parameters with simple Python dictionaries, and access the response data in the same way. |
| 21 | + |
| 22 | +## Installation |
| 23 | + |
| 24 | +To install the `requests` module, use the following command: |
| 25 | + |
| 26 | +pip install requests |
| 27 | + |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +Making a GET Request |
| 32 | + |
| 33 | +Here's how to make a simple GET request using the requests module: |
| 34 | + |
| 35 | +import requests |
| 36 | + |
| 37 | +## Making a GET request |
| 38 | +response = requests.get('https://api.github.com/') |
| 39 | + |
| 40 | + |
| 41 | +print(f'Status Code: {response.status_code}') |
| 42 | + |
| 43 | + |
| 44 | +print(f'Content: {response.content}') |
| 45 | + |
| 46 | + |
| 47 | +## Other HTTP Methods |
| 48 | +The requests module supports several other HTTP methods: |
| 49 | + |
| 50 | +response = requests.post('https://httpbin.org/post', data={'key': 'value'}) |
| 51 | +print(response.text) |
| 52 | + |
| 53 | +response = requests.put('https://httpbin.org/put', data={'key': 'value'}) |
| 54 | +print(response.text) |
| 55 | + |
| 56 | + |
| 57 | +## Response Object |
| 58 | +The response object contains useful information about the response. Here are some of the most commonly used attributes and methods: |
| 59 | + |
| 60 | +response = requests.get('https://api.github.com/') |
| 61 | + |
| 62 | +# Print response URL |
| 63 | +print(f'URL: {response.url}') |
| 64 | + |
| 65 | +# Print status code |
| 66 | +print(f'Status Code: {response.status_code}') |
| 67 | + |
| 68 | +# Print response headers |
| 69 | +print(f'Headers: {response.headers}') |
| 70 | + |
| 71 | +# Print response content (in bytes) |
| 72 | +print(f'Content: {response.content}') |
| 73 | + |
| 74 | +# Print response encoding |
| 75 | +print(f'Encoding: {response.encoding}') |
| 76 | + |
| 77 | +# Print response text (decoded content) |
| 78 | +print(f'Text: {response.text}') |
| 79 | + |
| 80 | +# Print cookies |
| 81 | +print(f'Cookies: {response.cookies}') |
| 82 | + |
| 83 | +# Print elapsed time |
| 84 | +print(f'Time Elapsed: {response.elapsed}') |
| 85 | + |
| 86 | +# Close the connection |
| 87 | +response.close() |
0 commit comments