Skip to content

Commit 5307019

Browse files
RM
1 parent 5a818a6 commit 5307019

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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.

contrib/advanced-python/image.png

62.2 KB
Loading

0 commit comments

Comments
 (0)